This question already has an answer here:
This may be an pointless question based on opinion, style, and context but I've been racking my brain trying to decide whether or not to make use of custom exceptions and would love to hear some pros and cons from anyone with experience from both approaches.
For example I'm trying to decide between a custom ObjectNotFoundException vs IllegalArgumentException/IllegalStateException.
(Please note that I am not intending to throw an exception whenever a service cannot find an object with a given identifier. We simply return a null in that case. I'm speaking of when an action is to be performed on an object with a given identifier. In this case, the object in question is expected to exist, so if it does not, we want to throw an exception.)
On one hand, a "GiraffeNotFoundException" is explicit and could be handled differently than a generic IllegalStateException, but then again I suppose that would only matter if such a differentiation is actually required. Another pro is that a custom message for such an exception can be set as the default message for this exception (reducing a bit of redundancy).
On the other hand, it might not be a good idea to clutter the source code with custom exceptions. Also with standard exceptions, the Assert class from spring can be used quite efficiently. There are no intentions of publishing our code as a third party library so there are no pressures for using standard exceptions from that end.
Any other thoughts?