Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put finally in nested try/catch?

How does finally work in nested try/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 outer try as well?

like image 915
Jim Avatar asked Dec 12 '12 08:12

Jim


People also ask

How do you use Finally in try catch?

finally statements combo handles errors without stopping JavaScript. The try statement defines the code block to run (to try). The catch statement defines a code block to handle any error. The finally statement defines a code block to run regardless of the result.

Can finally be nested?

No because the inner try block will not be reached when an exception occurs before and therefore the finally block is not reached either.

Can we write finally inside try catch block?

No, we cannot write any statements in between try, catch and finally blocks and these blocks form one unit.

Can we use try catch and finally together?

The try block cannot be present without either catch clause or finally clause. Any code cannot be present in between the try, catch, finally blocks.


2 Answers

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
}
like image 54
T.J. Crowder Avatar answered Nov 15 '22 12:11

T.J. Crowder


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.

like image 39
Thihara Avatar answered Nov 15 '22 13:11

Thihara