Your daily dose of Java and related technologies.
News, Releases, Reviews, Tutorials, Articles, Events, Contests, Comments, Code and much more.
This java weblog is maintained by Xyling Technologies, making life simpler.

Tuesday, September 05, 2006

Three Rules for Effective Exception Handling

Exceptions in Java provide a consistent mechanism for identifying and responding to error conditions. Effective exception handling will make your programs more robust and easier to debug. Exceptions are a tremendous debugging aid because they help answer these three questions:

What went wrong?
Where did it go wrong?
Why did it go wrong?

Here are the three rules for effective exception handling:

1. Be Specific (i.e. Define your custom exception to give more precise information about the exception that has occured).

2. Throw early (if you find an errorneous piece of information/code, throw the exception then and there instead of letting it flow to the next level). e.g. before opening a file check if the fileName string is null and throw an exception before a NullPointerException is thrown with a trace of FileNotFoundException.

3. Catch Late (if you encounter an exception, do not proceed with the remaining tasks if they are related/dependent on the earlier ones).

To read the full article, point to the title of this post.

[Resource-Type: Article, Category: Java/Exception Handling,XRating: 4,Level: All]