<p>I have been in both situations: </p>

<ul>
<li>Creating too many custom Exceptions</li>
<li>Using too many general Exception class</li>
</ul>

<p>In both cases the project started OK but soon became an overhead to maintain (and refactor).</p>

<p>So what is the best practice regarding the creation of your own Exception classes?</p>

[{'answer_id': 27000, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 2, 'selected': False, 'text': "<p>Basically, each job deserves an own exception. When you catch exceptions, you don't distinguish different instances, like you would normally do with objects, therefore you need different subtypes. Using too many custom exceptions is a case which I see hardly occurring.</p>\n\n<p>One advice would be to create exceptions as needed, and if it becomes apparent that one exception type is a duplicate of another, refactor the code by merging the two. Of course it helps if some thought goes into structuring exceptions from the beginning. But generally, use custom exceptions for all cases that have no 1:1 correspondence to existing, situation-specific exceptions.</p>\n\n<p>On the other hand, <code>NullPointerException</code>s and <code>IndexOutofBoundsException</code>s might actually often be appropriate. Don't catch these, though (except for logging) as they're a programming error which means that after throwing them, the program is in an undefined state.</p>\n"}, {'answer_id': 27041, 'author': 'Landon Kuhn', 'author_id': 1785, 'author_profile': 'https://Stackoverflow.com/users/1785', 'pm_score': 3, 'selected': False, 'text': '<p>My rule of thumb is when the client (the caller) might reasonably want to do something different, depending on the type of exception thrown, the additional exception types are warranted. More often than not, however, the extra exception types are not needed. For instance, if the caller is writing code like</p>\n\n<pre><code>try {\n     doIt();\n} catch (ExceptionType1 ex1) {\n     // do something useful\n} catch (ExceptionType2 ex2) {\n     // do the exact same useful thing that was done in the block above\n}\n</code></pre>\n\n<p>then clearly the additional exception types are not needed. All too often I see (or am forced to write) code like this because the code being called was overzealous in its creation of new exception types.</p>\n'}, {'answer_id': 27064, 'author': 'Josh Brown', 'author_id': 2030, 'author_profile': 'https://Stackoverflow.com/users/2030', 'pm_score': 6, 'selected': True, 'text': '<p><a href="http://www.javaspecialists.eu/" rel="noreferrer">The Java Specialists</a> wrote a post about <a href="http://www.javaspecialists.eu/archive/Issue162.html" rel="noreferrer">Exceptions in Java</a>, and in it they list a few "best practices" for creating Exceptions, summarized below:</p>\n\n<ul>\n<li><p>Don\'t Write Own Exceptions (there are lots of useful Exceptions that are already part of the Java API)</p></li>\n<li><p>Write Useful Exceptions (if you have to write your own Exceptions, make sure they provide useful information about the problem that occurred)</p></li>\n</ul>\n'}, {'answer_id': 27422, 'author': 'jjnguy', 'author_id': 2598, 'author_profile': 'https://Stackoverflow.com/users/2598', 'pm_score': 3, 'selected': False, 'text': "<p>If I can't find an exception that has a name describing what type of error was caused then I make my own.</p>\n\n<p>That's my rule-o-thumb.</p>\n"}, {'answer_id': 28748, 'author': 'shsteimer', 'author_id': 292, 'author_profile': 'https://Stackoverflow.com/users/292', 'pm_score': 2, 'selected': False, 'text': '<p>My own rule of thumb:</p>\n\n<p>I never throw Exception, except in unit tests when what you throw is irrelevant and theres no reason to spend any extra time on it.</p>\n\n<p>I create my own custom exception type for errors occuring in my custom business logic.  This exception type is used as much as possible for recasting other exceptions, except in cases where it makes sense for the client to have visibility into what actually occurred.</p>\n'}, {'answer_id': 216589, 'author': 'Mark Jaeger', 'author_id': 335648, 'author_profile': 'https://Stackoverflow.com/users/335648', 'pm_score': 4, 'selected': False, 'text': "<p>Don't do what the developers at my company did. Somebody created an [sic] InvalidArguementException that parallels java.lang.IllegalArgumentException, and we now use it in (literally) hundreds of classes. Both indicate that a method has been passed an illegal or inappropriate argument. Talk about a waste...</p>\n\n<p>Joshua Bloch covers this in <em>Effective Java Programming Language Guide</em> [my bible of first resort on Best Practices] <strong>Chapter 8. Exceptions</strong> <em>Item 42: Favor the use of standard exceptions</em>. Here's a bit of what he says,</p>\n\n<blockquote>\n  <p>Reusing preexisting exceptions has several benefits. Chief among these, it makes your API easier to learn and use because <strong>it matches established conventions with which programmers are already familiar</strong> [<em>my emphasis, not Bloch's</em>]. A close second is that programs using your API are easier to read because they aren't cluttered with unfamiliar exceptions. Finally, fewer exception classes mean a smaller memory footprint and less time spent loading classes.</p>\n  \n  <p>The most commonly reused exception is IllegalArgumentException. This is generally the exception to throw when the caller passes in an argument whose value is inappropriate. For example, this would be the exception to throw if the caller passed a negative number in a parameter representing the number of times some action were to be repeated.</p>\n</blockquote>\n\n<p>That said, you should <strong>never</strong> throw Exception itself. Java has a well-chosen, diverse and well-targeted bunch of built-in exceptions that cover most situations <strong>AND</strong> describe the exception that occurred well enough so that you can remedy the cause.</p>\n\n<p>Be friendly to the programmers who have to maintain your code in the future.</p>\n"}, {'answer_id': 21803904, 'author': 'Neelesh Salian', 'author_id': 2471578, 'author_profile': 'https://Stackoverflow.com/users/2471578', 'pm_score': 2, 'selected': False, 'text': '<p>While creating your own exception:</p>\n\n<ul>\n<li><p>All exceptions must be a child of the <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html" rel="nofollow">Throwable Class</a></p></li>\n<li><p>If you want to write a checked exception that is automatically enforced by the Handle or Declare Rule, you need to extend the <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html" rel="nofollow">Exception Class</a></p></li>\n<li><p>If you want to write a runtime execption, you need to extend the Runtime Exception Class.</p></li>\n</ul>\n'}, {'answer_id': 52793202, 'author': 'Sorter', 'author_id': 1097600, 'author_profile': 'https://Stackoverflow.com/users/1097600', 'pm_score': 0, 'selected': False, 'text': '<h3>Don\'t eat exceptions, throw them <a href="https://stackoverflow.com/a/921583/1097600">https://stackoverflow.com/a/921583/1097600</a></h3>\n\n<p>Avoid creating your own exception. Use the below ones that are already there.</p>\n\n<pre><code>IllegalStateException\nUnsupportedOperationException\nIllegalArgumentException\nNoSuchElementException\nNullPointerException\n</code></pre>\n\n<p>Throw unchecked exceptions.</p>\n\n<h3>Example</h3>\n\n<pre><code>public void validate(MyObject myObjectInstance) {\n    if (!myObjectList.contains(myObjectInstance))\n        throw new NoSuchElementException("object not present in list");\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/26984', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2644/']