Alex Rivera | Logout

When should I create a new Exception class

Asked 2009-12-31T17:51:38.350
23

I notice that a number of Java exception classes differ only in the name of the class and do not add any new functionality. Most exceptions for example just seem to override Exception() or Exception(String message). This goes against the tenets of inheritance ie:- inherit to add new functionality.

What are some good reasons to create a new Exception class?

Edit
Report

2 Answers

11

You can catch exceptions by type, and having an exception of a particular type that is otherwise identical to the base Exception class allows you to be precise in your exception handling.

I would argue that it does add new functionality directly by increasing the specificity of exception handling code.

answered 2009-12-31T17:54:39.063
0

You add a new exception class when a user of your API might need to catch it conditionally (or, for checked exceptions specify different, specific types in a throws).

answered 2009-12-31T17:55:26.267

Your Answer