exception handling patterns?

T

Timasmith

Hi,

Does anyone know of some good articles on exception handling. From my
own experience this is where I am at:

System (as in the application) defined exceptions should

a) Extend something like a system.lang.xxxException when you want to
defend but you dont want to force the method user to catch/throw the
exception
e.g. NullArgumentException extends NullPointerException

b) Extend Exception when you need the method user to handle the
specific exception
e.g. InvalidUserNameException extends Exception

c) Extend your own exceptions when you have a commonality that the
user could protect against
e.g. SQLException extends DataAccessException
e.g. DataParameterException extends DataAccessException

Not sure what else to do around Exception Handling other than handle
them gracefully.
 
O

Oliver Wong

Timasmith said:
Hi,

Does anyone know of some good articles on exception handling. From my
own experience this is where I am at:

System (as in the application) defined exceptions should

a) Extend something like a system.lang.xxxException when you want to
defend but you dont want to force the method user to catch/throw the
exception
e.g. NullArgumentException extends NullPointerException

b) Extend Exception when you need the method user to handle the
specific exception
e.g. InvalidUserNameException extends Exception

Your rule "A" should be changed so that the exceptions of interest
extend RuntimeException, or one of its subclasses. NullPointerException is a
subclass of RuntimeException, so the example still works.

Similarly, in "B" you could extend any exception which is not a
RuntimeException (Exception itself is an exception, but not a runtime
exception).

Also, don't be afraid to occasionally throw one of Sun's built-in
exceptions, instead of defining your own.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top