Chapter 6: Exceptions in Java

 < Day Day Up > 



6.1 Introduction

Exceptions occur when a part of a program, such as a method, encounters a problem that it cannot handle correctly. These problems are not necessarily bugs; they could be such situations as a user entering invalid input that cannot be handled in the current method. Often the programmer has made allowances for these problems, but to keep the methods in the program as generic as possible, programs are designed so that these problems are handled outside of the method where they occur.

Developing a good strategy for handling exceptions in a normal, non-concurrent program is not an easy or straightforward task. A number of decisions must be made when deciding on how to handle exceptions and errors. What problems should a method handle, and what problems should be referred to the calling method? What should the program do when an error is encountered? What strategies can be employed to try to correct errors, and which errors should the program try to correct? These are difficult questions that should be addressed before beginning to write a program, as a poorly designed or incorrectly implemented strategy for handling exceptions can destroy an otherwise well implemented component. Developing a strategy for handling and reporting exception is as important to implementing a component correctly as any other part of the design.

Developing an exception handling strategy for components is much more difficult. Propagating exceptions out of a component is not the same as propagating an exception out of an ordinary object because many threads share components; therefore, the exceptions and actions to handle them must be consistent with the various ways a thread might use a component. Also, exception handling is done on a per-thread basis, so unlike a procedural program that can use the default exception handling mechanism and simply die if an exception is raised, an unhandled exception in a concurrent program could cause a single thread to die but leave others running, resulting in safety and liveness problems in the other threads.

Unfortunately, exception handling is often something even experienced programmers add to a program or component as an afterthought. When a problem is encountered, the programmer simply does something to ameliorate the situation with little or no thought as to how it will impact the eventual usability of the component. Error handling, like all other aspects of programming, is something that must be planned and is not something that can be hacked into the system after it is implemented.

This chapter explains why it is important to consider carefully how error handling and reporting will be done in a program. It begins by showing some less robust error handling strategies that are often used and often are all that a programmer has available to use. The drawbacks of these mechanisms are pointed out, and the chapter then shows how exceptions are used in Java and how they can be applied to the construction of a component.



 < Day Day Up > 



Creating Components. Object Oriented, Concurrent, and Distributed Computing in Java
The .NET Developers Guide to Directory Services Programming
ISBN: 849314992
EAN: 2147483647
Year: 2003
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net