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
65
votes
8 answers
Multiple return statements without compiler error
This was an interview question:
public class Demo {
public static void main(String[] args) {
System.out.println(foo());
}
static String foo() {
try {
return "try ...";
} catch (Exception e) {
…
user2575725
60
votes
4 answers
Can we use "return" in finally block
Can we use return statement in finally block. Can this cause any problem?

Rakesh KR
- 6,357
- 5
- 40
- 55
57
votes
6 answers
How does the try catch finally block work?
In C#, how does a try catch finally block work?
So if there is an exception, I know that it will jump to the catch block and then jump to the finally block.
But what if there is no error, the catch block wont get run, but does the finally block get…

omega
- 40,311
- 81
- 251
- 474
52
votes
3 answers
In a finally block, can I tell if an exception has been thrown
Possible Duplicate:
Is it possible to detect if an exception occurred before I entered a finally block?
I have a workflow method that does things, and throws an exception if an error occurred. I want to add reporting metrics to my workflow. In…

Kevin
- 11,521
- 22
- 81
- 103
52
votes
4 answers
If I return out of a try/finally block in C# does the code in the finally always run?
It seems like it does as per some initial testing, but what I'd like to know is if it is guaranteed to return or if in some cases it can not return? This is critical for my application but I haven't found a use-case yet where it wouldn't return.
I'd…

zorg
- 523
- 1
- 4
- 6
50
votes
5 answers
What's the equivalent of finally in Swift
I try to use the error handling modeling in Swift2.
do {
try NSFileManager.defaultManager().removeItemAtPath("path")
} catch {
// ...
} finally {
// compiler error.
}
But it seems that there is no finally keyword out there.How can I…

tounaobun
- 14,570
- 9
- 53
- 75
50
votes
5 answers
IntelliJ IDE gives error when using Try-Catch with Resources
I am attempting to use JDK 7's "try-catch with resources" statement; IntelliJ highlights my resource line, saying
Try-with-resources are not supported at this language level.
When I try to compile, I get:
java: try-with-resources is not…

Yankee
- 1,011
- 1
- 9
- 18
49
votes
9 answers
Why use finally
I never properly understood the use of the finally statement. Can anyone tell me what the difference is between:
try {
a;
block;
off;
statements;
} catch (Exception e) {
handle;
exception;
e;
} finally {
do;
…

Martijn
- 11,964
- 12
- 50
- 96
47
votes
12 answers
Is a finally block without a catch block a java anti-pattern?
I just had a pretty painful troubleshooting experience in troubleshooting some code that looked like this:
try {
doSomeStuff()
doMore()
} finally {
doSomeOtherStuff()
}
The problem was difficult to troubleshoot because doSomeStuff() threw…

Jared
- 25,520
- 24
- 79
- 114
47
votes
4 answers
How is the keyword 'finally' meant to be used in PHP?
So, I have been reading about the exceptions today on the PHP online manual, and realize I have yet to understand the purpose or real necessity of the finally keyword. I have read some posts here, so my question is slightly different.
I understand…

ʎɹnɔɹǝW
- 811
- 2
- 8
- 14
35
votes
16 answers
What is the point of the finally block?
Syntax aside, what is the difference between
try {
}
catch() {
}
finally {
x = 3;
}
and
try {
}
catch() {
}
x = 3;
edit: in .NET 2.0?
so
try {
throw something maybe
x = 3
}
catch (...) {
x = 3
}
is behaviourally equivalent?

Keshi
- 377
- 4
- 5
34
votes
9 answers
What are the circumstances under which a finally {} block will NOT execute?
In a Java try{} ... catch{} ... finally{} block, code within the finally{} is generally considered "guaranteed" to run regardless of what occurs in the try/catch. However, I know of at least two circumstances under which it will not execute:
If…

asteri
- 11,402
- 13
- 60
- 84
32
votes
10 answers
Try-catch-finally and then again a try catch
I have often come across situations like :-
try{
...
stmts
...
}
catch(Exception ex) {
...
stmts
...
} finally {
connection.close // throws an exception
}
which still needs a try - catch block inside…

Ajay
- 7,378
- 18
- 57
- 75
30
votes
3 answers
Python: multiprocessing.map: If one process raises an exception, why aren't other processes' finally blocks called?
My understanding is that finally clauses must *always* be executed if the try has been entered.
import random
from multiprocessing import Pool
from time import sleep
def Process(x):
try:
print x
sleep(random.random())
raise…

Daniel Wagner-Hall
- 2,446
- 1
- 20
- 18
30
votes
4 answers
Does return "happen after" finally?
I am trying to convince myself that actions taken in the finally clause happen before the function return (in the memory consistency sense). From the JVM specification, it is clear that within a thread, program order is supposed to drive the happens…

Justin
- 4,437
- 6
- 32
- 52