Handling exceptions
When Java is told to throw an exception, it stops executing the current code block and begins jumping up levels until the exception is handled. That's how we moved from deep within the Scanner.java class' 909 line to line 11 of Exceptions.java, the piece in our code where, as far as we're concerned, the exception occurred. If our code were executed by another block of code, because we haven't handled this exception, instead of printing out to System.err, we'd simply throw the exception up another level. Due to this, they'd see line 11 of Exception.java in their stack trace.
However, sometimes it doesn't make sense to keep throwing an exception. Sometimes, we want to handle the exception case because we know what to do ...
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