This post originated from an RSS feed registered with Java Buzz
by Goldy Lukka.
Original Post: Designing with exceptions
Feed Title: Xyling Java Blogs
Feed URL: http://www.javablogs.xyling.com/thisWeek.rss
Feed Description: Your one stop source for Java Related Resources.
How do you decide when to use/throw exceptions and when not? How do you decide which exception to use? Checked or Unchecked? Should you define your own exception class or use an inbuilt (core Java API) one?
Hmm.. sounds quite like an extension to design patterns :) Well, the article (found at the tile of this post) provides some general guidelines that can help you use exceptions in those situations where you've decided they are appropriate.
Here is a collection of the exception guidelines put forth by this article:
* If your method encounters an abnormal condition that it can't handle, it should throw an exception.
* Avoid using exceptions to indicate conditions that can reasonably be expected as part of the normal functioning of the method.
* If your method discovers that the client has breached its contractual obligations (for example, by passing in bad input data), throw an unchecked exception.
* If your method is unable to fulfill its contract, throw either a checked or unchecked exception.
* If you are throwing an exception for an abnormal condition that you feel client programmers should consciously decide how to handle, throw a checked exception.
* Define or choose an already existing exception class for each kind of abnormal condition that may cause your method to throw an exception.
The article gives several examples from the Java API that illustrate appropriate uses of exceptions. A must read for all and esp. the designers/architects.