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.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.
You will see the first one. Finally block is executed always and as a last.