Java Reference
In-Depth Information
16.
The output would then be
try block entered:
Leaving try block.
End of example.
17.
41.9
DoubleException thrown!
18. Yes, you can define an exception class as a derived class of the class
IOException
.
19.
Second catch.
End of exercise.
20.
The output would then be
First catch.
End of exercise.
21.
The output would then be
Bingo!
End of exercise.
22.
Trying
Starting sampleMethod.
Catching.
End program.
23.
The output would then be
Trying
Starting sampleMethod.
Trying after call.
End program.
24.
public static void
doStuff(
int
n)
throws
Exception
{
if
(n < 0)
throw new
Exception("Negative number.");
}
25.
If a method throws an exception and the exception is not caught inside the
method, then the method invocation ends immediately after the exception is
thrown. If the method invocation is inside a
try
block, then the exception is
thrown to a matching
catch
block, if there is one. If there is no
catch
block
matching the exception, then the method invocation ends as soon as that excep-
tion is thrown.