I have been in both situations:
- Creating too many custom Exceptions
- Using too many general Exception class
In both cases the project started OK but soon became an overhead to maintain (and refactor).
So what is the best practice regarding the creation of your own Exception classes?
The Java Specialists wrote a post about Exceptions in Java, and in it they list a few ‘best practices’ for creating Exceptions, summarized below:
Don’t Write Own Exceptions (there are lots of useful Exceptions that are already part of the Java API)
Write Useful Exceptions (if you have to write your own Exceptions, make sure they provide useful information about the problem that occurred)