The general approach used in programming for dealing with exceptions is to divide the code into two sections:
The first section of code detects an exception. This involves identifying some instruction sequence that might generate or throw an exception.
The second section of code takes some action to deal with the exception. This is called handling the exception.
There is a wide variety of exceptions, depending on the abnormal condition that occurs. By default, KJP and Java do not handle all types of exceptions. When an exception occurs, the program aborts, and the runtime system displays the type of condition detected. For example: "Exception in thread main ArithmeticException: division by zero." The runtime system also prints a trace of the function calls.
There are two basic categories of exceptions: checked and unchecked. The first type of exceptions, checked, can be controlled syntactically. The compiler checks that the exception is handled in the program. These exceptions are very likely to occur in the program.
Some of the methods in the Java library packages throw various types of exceptions. These are considered checked exceptions. For example, the following function can throw an exception when invoked.
function mread throws IOException is . . . endfun mread
Unchecked exceptions are very difficult to detect at compile time. These exceptions do not have to be handled in the program.
There are two blocks of statements that are needed for detection and processing of exceptions. The first block is called a try block and it contains statements that might generate an exception. When an exception occurs on the try block, the second block, called the catch block, begins to execute immediately.
The parameter in the catch block is an object reference declaration of type Exception. When an exception occurs, this parameter is passed and can be used in the block to get information about the exception. Method getMessage, defined in class Exception, can be used to get a description of the exception. The general syntactic structure of these two blocks of statements is:
try begin 〈 statements 〉 endtry catch 〈 parameters 〉 begin 〈 statements 〉 endcatch
The catch block provides a name to the object reference of the exception object that is caught. With this object reference, the message of the exception object can be displayed and/or any other action can be implemented to handle the exception.
A variation of the problem that reads data for employees follows. The solution presented includes an exception that occurs when the user types the value of the age that is zero or negative. Class TestException implements the solution to the problem. The KJP code for this class follows.
description This program checks for an exception in the value of age. */ class TestException is public description This is the main function of the application. If the age is zero or negative, an exception is thrown and caught. */ function main is variables integer obj_age real increase real obj_salary string obj_name string lmessage // message for exception objects object emp_obj of class Employeec object lexecep_obj of class Exception begin display "Enter name: " read obj_name display "Enter age: " read obj_age // Check for exception try begin if obj_age <= 0 then create lexecep_obj of class Exception using "Exception: age negative or zero" throw lexecep_obj endif endtry catch parameters object excobj of class Exception begin set lmessage = call getMessage of excobj display lmessage display "Retrying . . . " display "Enter age: " read obj_age endcatch // continue with processing display "Enter salary: " read obj_salary create emp_obj of class Employeec using obj_salary, obj_age, obj_name set increase = call sal_increase of emp_obj set obj_salary = get_salary() of emp_obj display "Employee name: ", obj_name display "increase: ", increase, " new salary: ", obj_salary endfun main endclass TestException
In the program discussed, handling of the exception is carried by displaying information about the exception object, lexcep_obj, by invoking its method getMessage, and by executing the instructions that reread the value of age. All these statements appear in the catch block. If no exception occurs, the catch block does not execute, and the program proceeds normally.
On the CD | The KJP code that implements class TestException is stored in the file TestException.kpl, and the Java implementation is stored in the file TestException.java. |
When the user types a zero or negative value for the age, an exception is thrown. The exception is detected in the try block. To handle the exception, the statements in the catch block are executed. These statements display the message that explains and identifies the exception and allows the user to reenter the value for the age.
When the program executes and the user enters a "bad" value for the age, the program stops, displays the message, and resumes to let the user reenter the value for the age. Figure 15.1 shows the values used to test this program.
Figure 15.1: Execution of program with exception.