八卦天地 — 文学城精选

October 27, 2006

Three Rules for Effective Exception Handling

Filed under: Default

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.

Comments »

The URI to TrackBack this entry is: http://newprospect.blogsome.com/2006/10/27/three-rules-for-effective-exception-handling/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Get free blog up and running in minutes with Blogsome
Theme designed by Jay of onefinejay.com