Note that there are some explanatory texts on larger screens.

plurals
  1. POProgrammatically distinguish exceptions
    text
    copied!<p>The type of an exception is often enough to handle it properly (for example you try to open a file and get a <code>FileNotFoundException</code>). However there are cases where you might catch multiple exception of the same type. For example, an <code>IllegalArgumentException</code> that can be caused by more than one argument. The <code>IllegalArgumentException</code> does not add any additional methods (or public fields) to the <code>Throwable</code> interface (accoding to the online <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.html" rel="nofollow">javadoc</a>) which means that the only information that you can rely on are the nested exception (which may or may not exist) and the message (which is for human consumption).</p> <p>I don't like the idea of extending <code>IllegalArgumentException</code> to add structured information to it, because other people would have to learn the new class. And I don't like the idea of littering projects with very specific exception classes. Using the message field is also a bad idea because it's not meant for programmatic access.</p> <p>I think <code>IllegalArgumentException</code> should have included details such as the class function and argument(s) in question. And in general custom exceptions should provide additional detail (other then just their type) for a more fine grained exception handling.</p> <p>What are considered generally the best practices for designing exception classes, and handling exceptions of the same type?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload