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.
Questions tagged [try-catch-finally]
435 questions
28
votes
11 answers
try/finally without catch and return value
I have a program as following:
public class Main {
public static void main(String[] args)throws Exception
{
int res = test();
System.out.println("after call , res = " + res) ;
}
public static int test()throws…

Sam
- 6,770
- 7
- 50
- 91
28
votes
4 answers
Throw an exception in try catch block
try {
if (isFileDownloaded)
// do stuff
else
throw new CustomException()
}
catch (Exception e)
{
// something went wrong to save the error to log
}
finally
{
//release resources
}
My question is would the catch catches the…

Quincy
- 1,923
- 5
- 27
- 37
27
votes
8 answers
Try-catch-finally in java
In Java, will the finally block not get executed if we insert a return statement inside
the try block of a try-catch-finally ?

Nikhil
- 355
- 2
- 4
- 5
27
votes
5 answers
try-catch-finally with return after it
I know how try, catch & finally work (for most part), but I have one thing I was wondering: what happens with a return statement after a try-catch-finally, while we already had a return in the try (or catch)?
For example:
public boolean…

Kevin Cruijssen
- 9,153
- 9
- 61
- 135
24
votes
3 answers
Return in the Finally Block... Why not?
As MSDN mentions:
The code in a Finally block runs after
a Return statement in a Try or Catch
block is encountered, but before that
Return statement executes. In this
situation, a Return statement in the
Finally block executes before…

serhio
- 28,010
- 62
- 221
- 374
23
votes
2 answers
Why does the Java Compiler copy finally blocks?
When compiling the following code with a simple try/finally block, the Java Compiler produces the output below (viewed in the ASM Bytecode Viewer):
Code:
try
{
System.out.println("Attempting to divide by zero...");
System.out.println(1 /…

Clashsoft
- 11,553
- 5
- 40
- 79
21
votes
5 answers
What's the `finally` keyword for in PHP?
Consider these two examples

marxjohnson
- 830
- 7
- 21
18
votes
8 answers
Why does this "finally" execute?
If you run the code below it actually executes the finally after every call to the goto:
int i = 0;
Found:
i++;
try
{
throw new Exception();
}
catch (Exception)
{
goto Found;
}
finally
{
…

Kredns
- 36,461
- 52
- 152
- 203
16
votes
4 answers
Close file in finally block doesn't work
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line = null;
} catch (FileNotFoundException fnf) {
fnf.printStackTrace();
} finally {
fr.close();
}
The fr.close() shows an…

noMAD
- 7,744
- 19
- 56
- 94
16
votes
5 answers
Finally is not executed when in a Thread running in a Windows Service
Can anyone explain why this finally block is not executed? I have read posts about when to expect finally block not be executed, but this seems to be another case. This code needs TopShelf and log4net. I am running .net 4.5
I guess it must be the…

Stig
- 1,974
- 2
- 23
- 50
16
votes
9 answers
What is the reason for this finally clause containing close() calls
I'm studying the online java course, Introduction to programming Using Java.
In the chapter on I/O the code below is introduced with the following statement:
By the way, at the end of this program, you'll find our first useful example of a finally…

mallardz
- 1,070
- 11
- 21
16
votes
1 answer
Can I use catch(e if e instanceof SyntaxError) in Node.js's javascript?
I read about try catch(e if e instanceof ...) blocks on MDN, however, upon trying it in Node.js, I get a SyntaxError: Unexpected token if.
If this doesn't work, is there another way to catch specific exceptions, instead of everything that might…

lowerkey
- 8,105
- 17
- 68
- 102
15
votes
5 answers
Throw exception from Called function to the Caller Function's Catch Block
internal static string ReadCSVFile(string filePath)
{
try
{
...
...
}
catch(FileNotFoundException ex)
{
throw ex;
}
catch(Exception ex)
{
throw ex;
}
finally
{
...
…

Anubhav Ranjan
- 1,558
- 3
- 18
- 32
15
votes
5 answers
Does code in finally get run after a return in Objective-C?
Consider the following code:
@try {
if (something.notvalid)
{
return;
}
// do something else
} @catch (NSException *ex) {
// handle exception
} @finally {
NSLog(@"finally!");
}
If something is not valid and I return from within the…

Kevlar
- 8,804
- 9
- 55
- 81
15
votes
4 answers
Can I use try-catch-finally like this?
I'm using try-catch for years, but I never learned how and when to use finally, because I never understood the point of finally (I've read bad books)?
I want to ask you about use of finally in my case.
My code example should explain everything:
$s =…

Kamil
- 13,363
- 24
- 88
- 183