No.
The InputMismatchException
was not caught, so control leaves the program.
finally{}
block
Sometimes you have a block of code that should always execute, no matter what.
If an Exception
was thrown you want it to execute, and if
an Exception
was NOT thrown you want it to execute.
By using a finally
block, you can ensure that some statements
will always run, no matter what happened in the try
block.
Here is the try/catch/finally
structure.
try { // statements, some of which might // throw an exception } catch ( SomeExceptionType ex ) { // statements to handle this // type of exception } catch ( AnotherExceptionType ex ) { // statements to handle this // type of exception } // more catch blocks finally { // statements which will execute no matter // how the try block was exited and no matter // which catch block was executed (if any) } // Statements following the structure
There can only be one finally
block, and it must follow
the catch
blocks.
try
block exits normally (no exceptions occurred), then
control goes directly to
the finally
block.
After the finally
block is executed, the statement following it gets control.
try
block exits because of an Exception
which
is handled
by a catch
block, first
that block executes and then control goes to
the finally
block.
After the finally
block is executed
the statements following it get control.
try
block exits because of an Exception
which
is NOT handled
by a catch
block control goes directly to
the finally
block.
After the finally
block is executed
the Exception
is thrown to the caller
and control returns to the caller.
In summary: if control enters a try
block, then it will always enter the finally
block.
Does the finally
block always execute?