Exceptions: When Things Go Wrong

It is not a matter of IF but WHEN things will go wrong in a computer program. Sometimes there are bugs, errors of one form or another. There are also unforeseen use cases. You can never assume a computer program is perfect. Exception-Handling helps us to catch erroneous events and devise means of correcting them. We discuss this topic here since exception-handling can take more code than should be put into the main line of execution. In such cases, a method in an exception-handling class should be called. Exception handling mechanisms allow a program to continue executing, instead of terminating it abruptly, even if an error occurs in the program.

10.1 Introduction

OBJECTIVES
After studying this chapter, you will
  • Understand Java’s exception-handling mechanisms. 
  • Be able to use the Java try/catch statement. 
  • Know how to design effective exception handlers. 
  • Appreciate the importance of exception handling in program design.
  • Be able to design your own Exception subclasses.

OUTLINE
10.1 Introduction 
10.2 Handling Exceptional Conditions 
10.3 Java’s Exception Hierarchy 
10.4 Handling Exceptions Within a Program 
10.5 Error Handling and Robust Program Design 
10.6 Creating and Throwing Your Own Exceptions 
10.7 From the Java Library: javax.swing.JOptionPane 
Chapter Summary 
Solutions to Self-Study Exercises

Mistakes happen. Making mistakes is the norm rather than the exception. This is not to say that we make mistakes more often than we get it right. It is to say that (almost) nothing we do or build is ever perfectly correct, least of all computer software. No matter how well-designed a program is, there is always the chance that some kind of error will arise during its execution.

An exception is an erroneous or anomalous condition that arises while a program is running. Examples of such conditions that we have discussed in this text include attempting to divide by 0 (arithmetic exception), reading a decimal value when an integer is expected (number format exception), attempting to write to a file that doesn’t exist (I/O exception), or referring to a nonexistent character in a string (index out of bounds exception). The list of potential errors and anomalies is endless.

A well-designed program should include code to guard against errors and other exceptional conditions when they arise. This code should be incorporated into the program from the very first stages of its development. That way it can help identify problems during development. In Java, the preferred way of handling such conditions is to use exception handling, a divide-and-conquer approach that separates a program’s normal code from its error-handling code.

This chapter describes Java’s exception handling features. We begin by contrasting the traditional way of handling errors within a program with Java’s default exception-handling mechanism. We show how exceptions are raised (thrown) and handled (caught) within a program and identify the rules that apply to different kinds of exceptions. We then focus on some of the key design issues that govern when, where, and how to use exceptions in your programs. Finally, we show how to design and implement one’s own Exception subclass.


Source: R. Morelli and R. Walde, Trinity College
Creative Commons License This work is licensed under a Creative Commons Attribution 4.0 License.