This post originated from an RSS feed registered with Java Buzz
by Goldy Lukka.
Original Post: Three Rules for Effective Exception Handling
Feed Title: Xyling Java Blogs
Feed URL: http://www.javablogs.xyling.com/thisWeek.rss
Feed Description: Your one stop source for Java Related Resources.
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.