... 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access