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
2674
votes
52 answers
Does a finally block always get executed in Java?
Considering this code, can I be absolutely sure that the finally block always executes, no matter what something() is?
try {
something();
return success;
}
catch (Exception e) {
return failure;
}
finally {
…

jonny five
- 27,200
- 3
- 20
- 11
317
votes
11 answers
What happens if a finally block throws an exception?
If a finally block throws an exception, what exactly happens?
Specifically, what happens if the exception is thrown midway through a finally block. Do the rest of statements (after) in this block get invoked?
I am aware that exceptions will…

Jack Kada
- 24,474
- 29
- 82
- 106
205
votes
20 answers
Why is try {...} finally {...} good; try {...} catch{} bad?
I have seen people say that it is bad form to use catch with no arguments, especially if that catch doesn't do anything:
StreamReader reader=new StreamReader("myfile.txt");
try
{
int i = 5 / 0;
}
catch // No args, so it will catch any…

mbeckish
- 10,485
- 5
- 30
- 55
200
votes
3 answers
@try - catch block in Objective-C
Why doesn't @try block work?
It crashed the app, but it was supposed to be caught by the @try block.
NSString* test = [NSString stringWithString:@"ss"];
@try {
[test characterAtIndex:6];
}
@catch (NSException * e) {
NSLog(@"Exception:…

Alexandru Circus
- 5,478
- 7
- 52
- 89
189
votes
6 answers
Returning from a finally block in Java
I was surprised recently to find that it's possible to have a return statement in a finally block in Java.
It seems like lots of people think it's a bad thing to do as described in 'Don't return in a finally clause'. Scratching a little deeper, I…

Matt Sheppard
- 116,545
- 46
- 111
- 131
185
votes
6 answers
Does 'finally' always execute in Python?
For any possible try-finally block in Python, is it guaranteed that the finally block will always be executed?
For example, let’s say I return while in an except block:
try:
1/0
except ZeroDivisionError:
return
finally:
print("Does this…

Stevoisiak
- 23,794
- 27
- 122
- 225
155
votes
9 answers
Why does a return in `finally` override `try`?
How does a return statement inside a try/catch block work?
function example() {
try {
return true;
}
finally {
return false;
}
}
I'm expecting the output of this function to be true, but instead it is false!

bonfo
- 2,110
- 3
- 17
- 14
143
votes
6 answers
Is it bad practice to return from within a try catch finally block?
So I came across some code this morning that looked like this:
try
{
x = SomeThingDangerous();
return x;
}
catch (Exception ex)
{
throw new DangerousException(ex);
}
finally
{
CleanUpDangerousStuff();
}
Now this code compiles fine…

lomaxx
- 113,627
- 57
- 144
- 179
132
votes
11 answers
Java Try Catch Finally blocks without Catch
I'm reviewing some new code. The program has a try and a finally block only. Since the catch block is excluded, how does the try block work if it encounters an exception or anything throwable? Does it just go directly to the finally block?

NullPointer0x00
- 1,808
- 3
- 18
- 20
105
votes
14 answers
Why use finally instead of code after catch
Why do this
} catch (SQLException sqle) {
sqle.printStackTrace();
} finally {
cs.close();
rs.close();
}
Instead of this
} catch (SQLException sqle) {
sqle.printStackTrace();
}
rs.close();
cs.close();

124697
- 22,097
- 68
- 188
- 315
90
votes
6 answers
How does Java's System.exit() work with try/catch/finally blocks?
I'm aware of headaches that involve returning in try/catch/finally blocks - cases where the return in the finally is always the return for the method, even if a return in a try or catch block should be the one executed.
However, does the same apply…

Thomas Owens
- 114,398
- 98
- 311
- 431
85
votes
5 answers
'finally block does not complete normally' Eclipse warning
Eclipse give me that warning in the following code:
public int getTicket(int lotteryId, String player) {
try {
c = DriverManager.getConnection("jdbc:mysql://" + this.hostname + ":" + this.port + "/" + this.database, this.user,…

José D.
- 4,175
- 7
- 28
- 47
77
votes
8 answers
Behaviour of return statement in catch and finally
Please see the following code and explain the output behavior.
public class MyFinalTest {
public int doMethod(){
try{
throw new Exception();
}
catch(Exception ex){
return 5;
}
…

Ammu
- 5,067
- 9
- 34
- 34
72
votes
6 answers
How can I break from a try/catch block without throwing an exception in Java
I need a way to break from the middle of try/catch block without throwing an exception.
Something that is similar to the break and continue in for loops.
Is this possible?
I have been getting weird throughts about throwing a custom exception (naming…

Basil Musa
- 8,198
- 6
- 64
- 63
70
votes
14 answers
Why use Finally in Try ... Catch
I see that the Finally in Try .. Catch will always execute after any parts of the execution of the try catch block.
Is it any different to just skip the Finally section and just run it after, outside the try catch block?
Example 1, Try ... Catch ...…

awe
- 21,938
- 6
- 78
- 91