... terminates at line 31 and returns control to the statement in main
that invoked method1
(i.e., line 7). The try
block at lines 6–8 encloses this statement. The exception has not been handled, so the try
block terminates and the first matching catch
block (lines 9–26) catches and processes the exception. If there were no matching catch
blocks, and the exception is not declared in each method that throws it, a compilation error would occur—main does not have a throws
clause because main
catches the exception. Remember that this is not always the case—for unchecked exceptions, the application will compile, but it will run with unexpected results.
Obtaining Data from an Exception Object
All exceptions derive from class Throwable
, which has a printStackTrace ...
Get Java How to Program, Early Objects, 11th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.