I have question about try, catch and finally in Java. Consider the following scenario:
try{
//Some code here that throws IOExceotion
}
catch (IOException ex){
System.out.println("Line 1: IOException Encountered!");
throw ex;
}
finally {
System开发者_如何学Go.out.println("Line 2: I am always executed!");
}
What would be the output of the code snippet above? Am I going to see:
Line 1: IOException Encountered!
Line 2: I am always executed!
or would it be
Line 2: I am always executed!
Line 1: IOException Encountered!
Or would it be just (since we have a throw in the catch block)
Line 1: IOException Encountered!
Basically, I haven't found an example where there is a "throw" in the catch block and finally block following the catch block (like the example above). Can anyone shed some light on it?
Thanks.
Quoting the Java Language Specification, §14.20.4:
A try statement with a finally block is executed by first executing the try block. Then there is a choice:
* If execution of the try block completes normally, then the finally block is executed, and then there is a choice:
o If the finally block completes normally, then the try statement completes normally.
o If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S.
* If execution of the try block completes abruptly because of a throw of a value V, then there is a choice:
o If the run-time type of V is assignable to the parameter of any catch clause of the try statement, then the first (leftmost) such catch clause is selected. The value V is assigned to the parameter of the selected catch clause, and the Block of that catch clause is executed. Then there is a choice:
+ If the catch block completes normally, then the finally block is executed. Then there is a choice:
# If the finally block completes normally, then the try statement completes normally.
# If the finally block completes abruptly for any reason, then the try statement completes abruptly for the same reason.
+ If the catch block completes abruptly for reason R, then the finally block is executed. Then there is a choice:
# If the finally block completes normally, then the try statement completes abruptly for reason R.
# If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded).
o If the run-time type of V is not assignable to the parameter of any catch clause of the try statement, then the finally block is executed. Then there is a choice:
+ If the finally block completes normally, then the try statement completes abruptly because of a throw of the value V.
+ If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and the throw of value V is discarded and forgotten).
* If execution of the try block completes abruptly for any other reason R, then the finally block is executed. Then there is a choice:
o If the finally block completes normally, then the try statement completes abruptly for reason R.
o If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S (and reason R is discarded).
You will see the first one. Finally block is executed always and as a last.
Is this a pop quiz? Just kidding. You will see the first one if there is an exception. The finally block statement will always get executed, so that will be printed always.
精彩评论