Where to put finally in nested try/catch?
Solution 1
If you want the code in the finally
block to run no matter what happens in either block, put it on the outer try
. If you only want it to run no matter what happens within the first try
block, put it there:
try{ // try/catch #1
//code block #1
}
catch(SomeException e){
//code block #2
try{ // try/catch #2
//code block #3
}
catch(OtherException e){
//code block #4
}
finally {
// This code runs no matter what happens with try/catch #2 (so
// after code block #3 and/or #4, depending on whether there's
// an exception). It does NOT run after code block #1 if that
// block doesn't throw, does NOT run after code block #2 if
// that block DOES throw), and does not run if code block #1
// throws SomeOtherException (code block #5).
}
}
catch(SomeOtherException e){
//code block #5
}
finally {
// This code runs no matter what happens with EITHER
// try/catch #1 or try/catch #2, no matter what happens
// with any of the code blocks above, 1-5
}
More briefly:
try {
// covered by "outer" finally
}
catch (Exception1 ex) {
// covered by "outer" finally
try {
// Covered by both "inner" and "outer" finallys
}
catch (Exception2 ex) {
// Covered by both "inner" and "outer" finallys
}
catch (Exception3 ex) {
// Covered by both "inner" and "outer" finallys
}
finally { // "inner" finally
}
}
catch (Exception4 ex) {
// covered by "outer" finally
}
finally { // "outer" finally
}
Solution 2
It depends on the place where you want the code in finally block to execute.
try{ //Try ROOT
//code
}
catch(SomeException e){ //Catch ROOT ONE
//code
try{ //Try NEST
//code
}
catch(OtherException e){ //Catch NEST
//code
}
finally{
//This will execute after Try NEST and Catch NEST
}
}
catch(SomeOtherException e){ //Catch ROOT TWO
//code
}
finally{
//This will execute after try ROOT and Catch ROOT ONE and Catch ROOT TWO
}
There is no best place it's dependent on the functionality you desire. But if you want the finally block to run only after all the try catch blocks then you should place it after the final root catch block.
Solution 3
From Doc
The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.
Its better to put your Finally where it required.
try{
//code
}
catch(SomeException e){ // 1
try{ //Try Block
//code
}
catch(OtherException e){ //2
//code
}
finally{
//This will execute after Try Block and Catch Block
}
}
catch(SomeOtherException e){ //3
//code
}
finally{
//This will execute after 1 and 2 and 3
}
For more details take a look of following links.
Jim
Updated on June 11, 2022Comments
-
Jim almost 2 years
How does
finally
work in nestedtry/catch
?
E.g. for:try{ //code } catch(SomeException e){ //code try{ //code } catch(OtherException e){ //code } } catch(SomeOtherException e){ //code }
Where is the best place to put
finally
? Or should I put it in nested and outertry
as well?-
Uwe Plonus over 11 yearsIt is not clear what the result should be. The
finally
block can be placed on everytry-catch
. -
Jim over 11 yearsSo if I put the
finally
in the outertry
will it be executed?I am looking for the clearer way for this -
Uwe Plonus over 11 yearslook at the answers there are some good explanations.
-
-
Corbin over 11 years"Its better to put your Finally out side of all catch block." No, it's better to put it where it makes sense. If the finally only needs to run for the inner block, put it with the inner block. your statement also assumes that there is only 1 finally, which is not necessarily the case.