Using Specific Exception Types

(Continued from CodingJavaUnitExceptionTests...)

On my current project, we have gravitated away from specific exception types, which I don't care for. They argue an increase in the amount of code to maintain; I argue that it's easier to know what went wrong, especially when many things can go wrong. I wonder whether this is merely a matter of taste. -- JbRainsberger

I usually find it better to use a single exception type but include an in depth textual description within the exception. I find it very rare to actually catch specific exceptions for handling, except during a debug phase. For me a good description that can be displayed to the user is sufficient and often superior to relying on exception types.


Agreed. In addition to descriptive messages, if there's useful context to be captured at the point of raising the exception, consider adding them as constructor parameters to initialize attributes for your custom exception type. These might help in the diagnosis of problems when the exception is raised. --PaulHolser

EditText of this page (last edited September 8, 2004) or FindPage with title or text search