In which examples did the try block execute without
an exception?
|
1 |
In which examples did the try block throw
an ArithmeticException ?
|
2 |
In which examples did the try block throw
an unhandled exception (one with no catch block)?
|
3 |
In which examples did the finally block execute?
|
1, 2, 3 |
catch{}
Blocks
If there is a finally
block, then the
catch
blocks can be omitted:
try { // statements, some of which might // throw an exception } finally { // statements which will execute no matter // how the try block was exited. } // Statements following the structure
This might be done if you don't want to handle Exception
s in your method,
but you have a few statements that must execute no matter what
before control is returned to the caller.
This often happens if a method has a lock on some resource that it should
give up before exiting.
For example, if the method opens a file, it should (perhaps) close it
before exiting.
Why can't the "Statements following the structure" be used to close a file that the method has opened?