Designing with exceptions
Guidelines and tips on when and how to use exceptions
By Bill Venners, JavaWorld.com, 07/01/98
Page 6 of 6
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.
Next month
In next month's Design Techniques I'll continue the mini-series of articles focusing on class and object design. Next month's article, the sixth of this mini-series,
will discuss design guidelines that pertain to thread safety.
A request for reader participation
I encourage your comments, criticisms, suggestions, flames -- all kinds of feedback -- about the material presented in this
column. If you disagree with something, or have something to add, please let me know.
You can either participate in a discussion forum devoted to this material, enter a comment via the form at the bottom of the article, or e-mail me directly using the link
provided in my bio below.
About the author
Bill Venners has been writing software
professionally for 12 years. Based in Silicon Valley, he provides
software consulting and training
services under the name Artima
Software Company. Over the years he has developed software for
the consumer electronics, education, semiconductor, and life
insurance industries. He has programmed in many languages on many
platforms: assembly language on various microprocessors, C on Unix,
C++ on Windows, Java on the Web. He is author of the book: Inside the
Java Virtual Machine, published by McGraw-Hill.