How to Handle Exceptions in EJB

From the article on DeveloperWorks.

Principles of exception handling

The following are some of the generally accepted principles of exception handling:

1. If you can’t handle an exception, don’t catch it.

2. If you catch an exception, don’t swallow it.

3. Catch an exception as close as possible to its source.

4. Log an exception where you catch it, unless you plan to rethrow it.

5. Structure your methods according to how fine-grained your exception handling must be.

6. Use as many typed exceptions as you need, particularly for application exceptions.

Point 1 is obviously in conflict with Point 3. The practical solution is a trade-off between how close to the source you catch an exception and how far you let it fall before you’ve completely lost the intent or content of the original exception.

Note: These principles are not particular to EJB exception handling, although they are applied throughout the EJB exception-handling mechanisms.

Technorati Tags: ,

One Reply to “How to Handle Exceptions in EJB”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: