Handling Errors with Exceptions
Exceptions and How to Handle Them
In Part 1, you created the
DiveLog class with a constructor that builds the frame for the Dive Log application, a
JMenu, and initializes a
JTabbedPane object with six titled tabs. Each tab creates an object from a placeholder class. In Part 2, you designed the
Welcome class, which appears on the
Welcome pane. In Part 3, the
Diver.java class introduced encapsulation, illustrates how to collect user data and display the input on screen through the use event handling basics.
Part 4 reinforces these concepts, introduces the Log Dives pane, and shows you how to:
JFileChooserclass for naming new files.
JOptionPaneclass to create pop-up boxes that print warnings or provide information.
For this part of the tutorial, you need one image and the
Dives.java placeholder class that creates the Log Dives pane. You can use a different image other than the one provided here, but to prevent problems with layout, make the image the same size as the image provided.
Note: It's assumed you installed the Java TM 2 Platform, Standard Edition (J2SE TM ) on your system, and that you have completed Part 1, Part 2, and Part 3 of the Building an Application tutorial.
The Log Dives pane consists of multiple classes:
Dives.javaborder layout. The Save button creates a
JFileChooserdialog box and writes the data to a file that the user specifically names.
For now, create placeholder classes for the last three classes listed above.
Currently, each placeholder class does not have a constructor. Later in the tutorial, you define a constructor for each class.
Next, initialize the placeholder classes within the
Dives.java class constructor.
Creating a class to initialize other objects demonstrates one way to organize a complex panel. This is especially useful if you need to use several different layouts. You could create one huge class for all of that, but by breaking down a panel into several classes you can make changes to individual sections without affecting the code in the others.
The Log Dives panel is organized with the following classes that teach Java I/O specifics:
NorthPanel: demonstrates how to read from a file
CenterPanel: demonstrates how to write to a file
WestPanel: demonstrates how to read and write objects to a file
The process of reading from and writing to files is error prone. The application might try to open a file that doesn't exist, or attempt to write to a file that is read-only. Any number of things can go wrong.
Before learning to read from and write to files, you need to understand how to prepare for and handle errors and problems that may occur.
When an application encounters an error or unexpected condition, it sometimes terminates, displaying a message on the console. In some cases, the application doesn't start.
For instance, if you typed the following at the command line:
You'd get this error message, assuming you don't have a file called
javac DisplayError.java error: cannot read: DisplayError.java 1 error
The compiler couldn't find the named file and printed this message to the screen. These types of console messages aren't always helpful, and often you don't want the application to terminate, but continue onto another bit of code you've written. To force an application to do something in the event of a problem, you must first catch and handle the problem condition. The Java programming language has special classes and syntax to deal with potential errors and problems.
Applications can generate many types of unexpected conditions. For instance, you may want the user to enter an
int, but instead the user enters a
String. The user may request the name of a file that does not exist, or your program logic may erroneously try to divide by zero. Unexpected conditions in an application are referred to as exceptions in the Java programming language. In other words, an exception is a way to indicate an abnormal condition in an application.
The steps for handling those exceptions are as follows:
There are two types of conditions:
Errors indicate fatal errors, such as a system out of memory error. These are difficult to recover from. An error indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.
On the other hand, exceptions indicate non-fatal conditions that you can frequently catch and handle.
Exceptions and errors are objects. The classes used to create the objects are subclasses of the
Throwable class. You don't use
Throwable directly, but instead use one of its subclasses, such as
Exception object is created by the code at the point where the error or condition occurred. This object holds information necessary to describe the condition. The
Exception object is passed to the handling block of code, where you write instruction for what you want to happen in the event this error or condition should occur.
For instance, suppose you created a form for the user to fill in. You request that the user type the name of a file to read. Since you know it's possible that file may not exist on the user's system, you need to write code that catches and handles this condition. When the user types in the name of a nonexistent file, instead of the program terminating, you write code that pops up a box, letting the user know that file does not exist and ask if it should be created. Then you need to write code to handle a Yes or No response. Or perhaps you want to create a warning of some sort. At any rate, you've prepared for a possible condition and written code to handle it.
More specific exception classes are derived from the
Error classes. Class
Throwable and its subclasses have two constructors, one that takes no arguments and one that takes a
String argument that can be used to produce an error message.
To prepare for possible conditions, where should you write the code to open a file for reading?