When and When Not to Use Exceptions
Because throwing, catching, and declaring exceptions are related concepts and can be very confusing, here's a quick summary of when to do what.
When to Use Exceptions
You can do one of three things if your method calls another method that has a throws clause:
Deal with the exception by using try and catch statements
Pass the exception up the calling chain by adding your own throws clause to your method definition
Perform both of the preceding methods by catching the exception using catch and then explicitly rethrowing it using throw
In cases where a method throws more than one exception, you can handle each of those exceptions differently. For example, you might catch some of those exceptions while allowing others ...
Get Sams Teach Yourself Java 2 in 21 Days, Second 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.