Questions tagged [try-catch-finally]

A common usage of catch and finally together is to obtain and use resources in a try block, deal with exceptional circumstances in a catch block, and release the resources in the finally block.

435 questions
0
votes
2 answers

Avoiding an empty catch clause

I have a function which checks to see if the user has made any changes, and if so, warns them of this fact. When they choose to discard their changes, I have another function which a) restores the state to pre-edit, and b) updates a global object…
Chris Middleton
  • 5,654
  • 5
  • 31
  • 68
0
votes
3 answers

Add a class to the catch statement

I have a problem when I try to add a class in the catch statement. When I click the button to confirm, javascript show me the error with the class successfully added, but when I reclick the button, without refreshing the page, the class added is no…
user3063608
0
votes
4 answers

Try Catch - Finally in If statement, how to go on after?

how can I do that ? void x() {.... if (...) {try {} catch (ComException com) { throw com} finally // in any case, executed fine! {...instructions.......} …
KitAndKat
  • 953
  • 3
  • 14
  • 29
0
votes
2 answers

Try-Catch-Finally - Final Block not recognising variable

firstly I know I should be using a try-catch with resources, however I don't currently have the most up to date JDK on my system. I have the following code below, and am trying to ensure the resource reader is closed using the finally block, however…
Dave0504
  • 1,057
  • 11
  • 30
0
votes
2 answers

return status from C finally and C2220

I i am using code as NTSTATUS Register (_In_ FLT_FILESYSTEM_TYPE VolumeFilesystemType) { NTSTATUS status = STATUS_FLT_DO_NOT_ATTACH; try { if (VolumeFilesystemType != FLT_FSTYPE_NTFS) { status = STATUS_NOT_SUPPORTED; …
Brans Ds
  • 4,039
  • 35
  • 64
0
votes
3 answers

Java 'finally' exception throwing rules

Lets say I have a class that has two OutputStream members: public class A { OutputStream os1 = ... OutputStream os2 = ... ... } Now, in this class, I have a cleanup() method which must at least attempt to close both streams: public…
Ben
  • 7,692
  • 15
  • 49
  • 64
0
votes
5 answers

About finally block

I know finally's purpose is to guarantee that some instructions or cleanup code get executed. But what is the actuall difference of using finally or putting the code after the catch block. I mean, give me an example of a case where you need to use…
Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
0
votes
1 answer

how to use 'try catch finally' block to display message when mysql query is executed successfully

I have been using a 'try catch finally' in the code for a C# Windows form to query a MySQL database. The code works well: when the catch block flags errors, a message box will display error messages. In the finally block, I coded a message box that…
0
votes
4 answers

Try/Catch Blocks and Following Statements

I'm having some issues with my code below. In this program, I input the description, units, and price of an item. I made two custom exception classes for the possibility of the user inputting a negative number. When I run the program, the catch…
Saad
  • 49,729
  • 21
  • 73
  • 112
0
votes
2 answers

Handling multiple exceptions in java

My abandon() may throw AbandonException. While handling the exception I have to recall the same method if there some element left in the Vector. How should I proceed? And if I am not thinking straight, what would be the best solution? if (i + 1 <…
LonsomeHell
  • 573
  • 1
  • 7
  • 29
0
votes
1 answer

Try-Catch-Finally execution order

Here is the code to demonstrate my point (java): public static int getSize(List list) { System.out.println("begin"); try { System.out.println("get list size"); return list.size(); } catch (Exception e) …
LarsChung
  • 703
  • 5
  • 10
  • 24
0
votes
6 answers

Using finally keyword on InputStream#close()

Basically I am not really sure what is the correct usage of the finally keyword, I only know the textual definition: Guarantees a code will be executed cause sometimes it doesn't. So I was hoping I could get some directions on this particular…
Christopher Francisco
  • 15,672
  • 28
  • 94
  • 206
0
votes
1 answer

Does Backbone have a finally-Callback?

I am currently creating a Backbone.js and jquery Mobile web application and I have some calls to Backbone.model.fetch(). I have the proper success and error-callbacks, but there is some code that I want to execute no matter if the fetch was…
0
votes
2 answers

Java - Reader cannot be resolved

I'm having a little trouble with my java syntax and I'm stumped to what's wrong. The problem lies in my BufferedReader in: "finally{reader.close();}" where it's saying reader cannot be resolved, but in the "try" immediately before it, it's resolved…
reekdev
  • 13
  • 1
  • 8
0
votes
2 answers

Try Catch Finally doesn't return a value on all code paths

I was working doing a lot of refactoring in a legacy code, and I found this: Public Function GetDocumentTypes() As DataSet Dim ds As DataSet = Nothing Try ds = SqlHelper.ExecuteDataset(Conn, "USP_DocumentTypes_Get") …