Exercises

Included below are short-answer and programming exercises. Answers are provided for those exercises whose exercise number is a hyperlink. Because college faculty use these exercises in their exams, we have provided answers to roughly half of the exercises included here.


14.16 Under what circumstances would you use the following statement?

catch ( Exception e ) { throw e; }

14.17 List the benefits of exception handling over conventional means of error processing.

14.18 Describe an object-oriented technique for handling related exceptions.

14.19 Until this chapter, we have found that dealing with errors detected by constructors is a bit awkward. Explain why exception handling is an effective means for dealing with constructor failure.

14.20 Suppose a program throws an exception and the appropriate exception handler begins executing. Now suppose that the exception handler itself throws the same exception. Does this create an infinite recursion? Explain your answer.

14.21 Use inheritance to create an exception superclass and various exception subclasses. Write a program to demonstrate that the catch specifying the superclass catches subclass exceptions.

14.22 Write a Java program that shows that all finalizers for objects constructed in a block are not necessarily called after an exception is thrown from that block.

14.23 Write a Java program that demonstrates how various exceptions are caught with

catch ( Exception e )

14.24 Write a Java program that shows that the order of exception handlers is important. If you try to catch a superclass exception type before a subclass type, the compiler should generate errors. Explain why these errors occur.

14.25 Write a Java program that shows a constructor passing information about constructor failure to an exception handler after a try block.

14.26 Write a Java program that illustrates rethrowing an exception.

14.27 Write a Java program which shows that a method with its own try block does not have to catch every possible error generated within the try. Some exceptions can slip through to, and be handled in, other scopes.


Selected Answers

Included below are answers to approximately half the of the exercises in the Cyber Classroom. We are not able to include answers to every exercise because college faculty use these exercises in their classroom exams.


14.16 Under what circumstances would you use the following statement?

catch ( Exception e ) { throw e; }
ANS: The preceding is used to catch any exception and rethrow it for handling by an exception handler in a previous method call.

14.18 Describe an object-oriented technique for handling related exceptions.
ANS: Create a superclass for all the related exceptions. In this class, place all functionality that is common to the related exceptions. From this class, derive all the related exception classes. Once the exception class hierarchy is created, exceptions from the hierarchy can be caught as the superclass exception type or as one of the subclass class exception types.

14.19 Until this chapter, we have found that dealing with errors detected by constructors is a bit awkward. Explain why exception handling is an effective means for dealing with constructor failure.
ANS: A thrown exception passes to the outside world the information about the failed constructor and the responsibility to deal with the failure. Exceptions thrown in constructors cause objects built as part of the object being constructed to be marked for eventual garbage collection.

14.21

// Exercise 14.21 Solution
// Demo.java
// Program demonstrates that the exception
// superclass will catch the subclass exceptions
import javax.swing.*;

class ExceptionA extends Exception {}

class ExceptionB extends ExceptionA {}

class ExceptionC extends ExceptionB {}

public class Demo {

   public static void main( String args[] )
   {
      try {
         throw new ExceptionC();
      }
      catch( ExceptionA a ) {
         JOptionPane.showMessageDialog(
           null, a.toString(), "Exception",
           JOptionPane.INFORMATION_MESSAGE );
      }

      try {
         throw new ExceptionB();
      }
      catch( ExceptionA b ) {
           JOptionPane.showMessageDialog(
             null, b.toString(), "Exception",
             JOptionPane.INFORMATION_MESSAGE );
      }
      System.exit( 0 );
   }
}

14.26

// Exercise 14.26 Solution
// Demo4.java
// Program demonstrates rethrowing an exception
import javax.swing.*;

public class Demo4 {

   public static void main( String args[] )
   {
      try {
         someMethod();
      }
      catch( Exception e ) {
         JOptionPane.showMessageDialog(
           null, "Handled in main: " + e, "Exception",
           JOptionPane.INFORMATION_MESSAGE );
      }
      System.exit( 0 );
   }

   public static void someMethod() throws Exception
   {
      try {
         someMethod2();
      }
      catch( Exception e ) {
         throw e;        // rethrow the exception
      }
   }

   public static void someMethod2() throws Exception
   {  throw new Exception();  }
}