Skip to main content
Logo image

Java, Java, Java: Object-Oriented Problem Solving, 2024E

Section 10.8 Chapter Summary

Subsection 10.8.1 Technical Terms

Table 10.8.1.
catch block catch an exception checked exception
dialog box dynamic scope error dialog
exception exception handling finally block
method call stack method stack trace modal dialog
static scope throw an exception try block
unchecked exception

Subsection 10.8.2 The Try/Catch Statement

The try/catch/finally statement has the following syntax:
try {
    // Block of statements
    // At least one of which may throw an exception
    if ( * Some condition obtains */ )
        throw new ExceptionName();} catch (ExceptionName ParameterName) {
    // Block of statements to be executed
    // If the ExceptionName exception is thrown in try}
..} catch (ExceptionName2 ParameterName) {
    // Block of statements to be executed
    // If the ExceptionName2 exception is thrown in try
  } finally {
    // Optional block of statements that is executed
    // Whether an exception is thrown or not
  }
The try block is meant to include a statement or statements that might throw an exception. The catch blocks—there can be one or more—are meant to handle exceptions that are thrown in the try block. A catch block will handle any exception that matches its parameter class, including subclasses of that class. The finally block is optional. It will be executed whether an exception is thrown or not. If an exception is thrown in the try block, the try block is exited permanently.
The throw statement inside the try block is there to illustrate how throw can be used. You will usually not see a throw statement in a try block, because most throws are done from within Java library methods, which are called from a try block.

Subsection 10.8.3 Summary of Important Points

  • In Java, when an error or exceptional condition occurs, you throw an Exception, which is caught by special code known as an exception handler. A throw statement—throw new Exception()—is used to throw an exception.
  • A try block is a block of statements containing one or more statements that may throw an exception. Embedding a statement in a try block indicates your awareness that it might throw an exception and your intention to handle the exception.
  • Java distinguishes between checked and unchecked exceptions. Checked exceptions must either be caught by the method in which they occur or you must declare that the method containing that statement throws the exception.
  • Unchecked exceptions are those that belong to subclasses of RuntimeException. If they are left uncaught, they will be handled by Java’s default exception handlers.
  • A catch block is a block of statements that handles the exceptions that match its parameter. A catch block can only follow a try block, and there may be more than one catch block for each try block.
  • The try/catch syntax allows you to separate the normal parts of an algorithm from special code meant to handle errors and exceptional conditions.
  • A method stack trace is a trace of the method calls that have led to the execution of a particular statement in the program. The Exception.printStackTrace() method can be called by exception handlers to print a trace of exactly how the program reached the statement that threw the exception. Static scoping refers to how the text of the program is arranged. If a variable is declared within a method or a block, its static scope is confined to that method or block. Dynamic scoping refers to how the program is executed. A statement is within the dynamic scope of a method or block if it is called from that method or block, or if it is called by some other method that was called from that method or block.
  • When searching for a catch block to handle an exception thrown by a statement, Java searches upward through the statement’s static scope and backward through its dynamic scope until it finds a matching catch block. If none is found, the Java Virtual Machine will handle the exception itself by printing an error message and a method stack trace.
  • Many Java library methods throw exceptions when an error occurs. These throw statements do not appear in the program. For example, Java’s integer division operator will throw an ArithmeticException if an attempt is made to divide by zero.
  • Generally, there are four ways to handle an exception: (1) Let Java handle it; (2) fix the problem that led to the exception and resume the program; (3) report the problem and resume the program; and (4) print an error message and terminate the program. Most erroneous conditions reported by exceptions are difficult or impossible to fix.
  • A finally statement is an optional part of a try/catch block. Statements contained in a finally block will be executed whether an exception is raised or not.
  • A well-designed program should use exception handling to deal with truly exceptional conditions, not as a means of normal program control.
  • User-defined exceptions can be defined by extending the Exception class or one of its subclasses.

Solutions 10.8.4 Solutions to Self-Study Exercises

10.3 Java’s Exception Hierarchy
10.3.1 Java Predefined Exceptions

Self-Study Exercise
10.3.1.1. Exceptions Matching.
Solution.
  1. Integer.parseInt("26.2"); ==> NumberFormatException
  2. String s; s.indexOf('a'); ==> NullPointerException
  3. String s = "hello"; s.charAt(5); ==> StringIndexOutOfBoundsException

10.3.6 Self-Study Exercises

Exercise 10.3.8. Unchecked Exceptions.
Solution.
The unchecked exceptions are IndexOutOfBoundsException, NumberFormatException, and NullPointerException, because these are subclasses of RuntimeException. The others are checked exceptions.

10.4 Handling Exceptions Within a Program
10.4.7 Self-Study Exercises

10.4.7.1. Catches.
Solution.
An ArrayIndexOutOfBoundsException could be handled by the handlers in RunTimeException, IndexOutOfBoundsException, or Exception, because their classes are all superclasses of ArrayIndexOutOfBoundsException.
10.4.7.2. Exception Output.
Solution.
If Math.random() in MyClass2 returns 0.98 and then 0.44, the program will generate the following output:
0.98 is out of range
Note that because the out-of-range error occurs in method1(), method2() is not called at all. The following stack trace would be printed:
java.lang.ArithmeticException: 0.98 is out of range
    at MyClass2.method1(MyClass2.java:3)
    at MyClass2.main(MyClass2.java:15)
10.4.7.3. Exception Output 2.
Solution.
If Math.random() in MyClass2 returns 0.44 and then 0.98, the program will generate the following output:
Hello 0.44
0.98 is out of range
The following stack trace would be printed:
java.lang.ArithmeticException: 0.98 is out of range
    at MyClass2.method2(MyClass2.java:8)
    at MyClass2.main(MyClass2.java:16)
10.4.7.4. Divide by Zero Exception.
Solution.
The divide-by-zero error in BadDivide occurs in the expression n/d in Method2(). It would generate the following stack trace:
java.lang.ArithmeticException: divide by zero
at BadDivide.method2(BadDivide.java:7)
at BadDivide.method1(BadDivide.java:3)
at BadDivide.main(BadDivide.java:13)
10.4.7.5. Modify Divide by Zero.
Solution.
The following version of BadDivide.method2() will handle the divide-by-zero error itself:
public void method2 (int n, int d) {
    try {
        System.out.println(n / d);
    } catch (ArithmeticException e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
        System.exit(0);
    }
  }
10.4.7.6. Exception Output 3.
Solution.
If someValue equals 1000, the code segment will print
Entering try block
ERROR: 1000 is too large
10.4.7.7. Exception Output 4.
Solution.
If someValue equals 50, the code segment will print
Entering try block
Exiting try block
10.4.7.8. Try/Catch Block.
Solution.
try {
  if (X < 0)
     throw new Exception("ERROR: Negative value in X coordinate");
} catch (Exception e) {
     System.out.println( e.getMessage() );
}

10.5 Error Handling and Robust Program Design
10.5.5 Self-Study Exercise

Exercise 10.5.19. Resume or Terminate.
Solution.
  1. This is a computer game, so one way to handle this problem would be to generate a message into a log file and resume the game.
  2. You should probably terminate. You would have to decide whether it would be more harmful or dangerous to continue production than not.
  3. The program could report the security violation to the user and to the system manager and then keep accepting user input.

10.6 Creating and Throwing Your Own Exceptions
10.6.2 IntFieldClass

Self-Study Exercises
10.6.2.1. FieldIsEmptyException.
Solution.
public class FieldIsEmptyException extends Exception {
   public FieldIsEmptyException () {
       super("The input field is empty ");
   }
 }
10.6.2.2. IntField.getInt().
Solution.
public int getInt() {
 int num = 0;
 try {
     String data = getText();
     if (data.equals(""))
         throw new FieldIsEmptyException();
     num = Integer.parseInt( getText() );
     if (num > bound)
         throw new IntOutOfRangeException(bound);
 } catch (FieldIsEmptyException e) {
     System.out.println("Error: " + e.getMessage() );
 } catch (NumberFormatException e) {
   System.out.println("Error: You must input an integer.
                                    Please try again.");
 } catch (IntOutOfRangeException e) {
     System.out.println(e.getMessage());
     return 0;
 }
 return num;
}
You have attempted of activities on this page.