Try catch finally execution flow
If an exception is thrown in catch block of try-catch, then will the开发者_开发技巧 finally block be called?
try
{
//some thing which throws error
}
catch
{
//again some thing throws error
}
finally
{
//final clean up
}
Here, will finally be called?
Thanks, Ashwani
In at least Java and C#, the finally
block is always executed regardless how the try block exits.
If the answer were false, the finally
construct would provide no advantage over simply including the code at the end of the try block.
Even though there's an exception or not in the try
block, finally
block is bound to execute. That's the way finally
was designed.
If there's an exception in the try
block, then control will be transferred to the catch
block to match the generated exception and will then switch over the control to the finally
block.
But in this case, if there isn't any exception generated in the try
block, the control won't be transferred to the catch
block because there wasn't any exception to catch
.
But as I said earlier, the finally
block is again bound to execute.
So, finally, the finally block will always be executed
The answer is yes. But what happens after the Finally block? To illustrate the full flow semantics, consider the following:
private int TryCatchFinallyFlow()
{
int i, j = 0; // set j to non-zero
try
{
i = 1 / j;
goto lSkip; // comment out;
return i; // comment out
lSkip:;
}
catch (Exception)
{
i = 2;
throw; // comment out
}
finally
{
i = 3;
}
i = 4;
return i;
}
private void button1_Click(object sender, EventArgs e)
{
int i;
try
{
i = TryCatchFinallyFlow();
}
catch (Exception)
{
i = -1;
}
MessageBox.Show(i.ToString());
}
}
You can experiment with j being 0 or not zero, and with commenting out the three "comment out" lines. Step through in debug mode.
This will show you fully how the flows work.
In every language I know: Yes.
No if there is an exception in both the try and catch blocks as in your example above, control does not pass to the finally block. Illustration:
try
{
//exception -> control goes to catch block.
}
catch
{
//again exception -> exits showing error
}
finally
{
//control does not reach here
}
NOTE: If however this above code is wrapped inside some function and there is proper exception handling i.e. try... catch block in the calling function, then finally block does get executed and control moves from catch above to the calling function's catch. Illustration:
try
{
//exception -> goes to catch block.
}
catch
{
//again exception -> goes to finally block
}
finally
{
//code here is executed -> goes to calling function's catch block
}
Yes, finally block will be executed if there is an exception in catch block also.
This is the code of testing with debugging statements.
Code
public class Demo{
public static void main(String args[]){
int i,j,ans;
try{
System.out.println("Debug statement 1");
i = 9;
ans = i / 0;
System.out.println("Debug statement 2");
}
catch(Exception e){
System.out.println("Debug statement 3");
j=5;
ans = j/0;
System.out.println("Debug statement 4");
}
finally{
System.out.println("Finally Executed");
}
}
}
Output
Debug statement 1
Debug statement 3
Finally Executed
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Demo.main(Demo.java:15)
精彩评论