Chapter 18: Exceptions

The exercises here are taken from my forthcoming book, The Java Developer's Resource.

Quiz

  1. Can there be a try block without a matching catch block?

    Yes, if there is a finally block. try without catch or finally is a compile-time error.

  2. Can there be a catch block without a matching try block?

    No. This is a compile-time error.

  3. Can there be a finally statement that is not attached to a try-catch block?

    No. This is a compile-time error.

  4. When are exception objects created?

    In general when an unexpected condition occurs. In specific, when you use throw to send a new exception, when the Java library uses throw to send a new exception, or when the Java runtime signals a RuntimeException.

  5. Does an exception have to be handled by the same try-catch block that threw it?

    Not necessarily. If an exception isn't caught by the nearest try-catch block, it can be caught higher up. If it's a runtime exception it may not be caught at all.

  6. How can you make sure you catch every exception that a statement may throw?

    Use this catch block as the last one in your hierarchy:

    catch (Exception e) {
    
    }
    
  7. If an exception isn't caught in a user-defined class, where does it go and what does it do?

    If you're in an applet it may be caught in the applet viewer or web browser. However, the most likely result is that the thread in which the exception was thrown will be killed.

  8. Suppose you've defined an exception called NotANumberException(). Now suppose that NotAPositiveNumberException extends NotANumberException. Will

    	catch (NotANumberException) {
      	System.out.println("Caught a NotANumberException");
    	}
    catch a NotAPositiveNumberException?

    Yes. Superclasses always catch their subclasses.

    Now suppose you have the following catch clause and throw a NotAPositiveNumberException. What happens?

    	catch (NotANumberException) {
      	System.out.println("Caught a NotANumberException");
    	}
    	catch (NotAPositiveNumberException) {
      	System.out.println("Caught a NotAPositiveNumberException");
    	}
    	
    "Caught a NotANumberException" is printed on System.out.

  9. In general which catch statement should come first? The one that catches the subclass or the one that catches the superclass? Why?

    The one that catches the subclass. If the catch block that catches a superclass comes first, then the subclass will never be seen. You should move from the most specific (the subclass) to the most general (the superclass).

  10. What do you have to do to have an exception handled by both its own catch statement and by its superclass's catch statement?

    Nest try statements and rethrow the exception inside the first catch block, like this

    try {
      try {
     
        // statements that may fail
      }
      catch (SubclassException e) {
        // handle subclass
        throw(e);
      }
    }
    catch (superclass Exception e) {
      // handle superclass
      
    }
    
  11. What's the difference between throw and throws?

    The throw keyword throws a new exception. The throws keyword declares that a method may throw a particular exception.

Exercises

  1. Chances are very good that some of the programs you've written before have encountered exceptions. Since you didn't catch the exceptions they simply halted the execution of your code. Go back to those programs and exception handling.

  2. Write a class that keeps a running total of all characters passed to it (one at a time) and throws an exception if it is passed a non-alphabetic character.

    The Character.isJavaLetter() method requires Java 1.1.

    public class NotALetterException extends Exception {
    
      static int count=0;
      String msg;
      
      
      public NotALetterException(String s) {
        msg = s;
      }
      
    
      public static void countLetter(char c) throws NotALetterException {
      
    
        if (Character.isJavaLetter(c)) {
          count++;
        }
        else {
          throw new NotALetterException(c + " is not a letter"); 
        }
      
      
      }
    
    }


[ Exercises | Cafe Au Lait | Books | Trade Shows | Links | FAQ | Tutorial | User Groups ]

Copyright 1996 Elliotte Rusty Harold
elharo@sunsite.unc.edu
Last Modified August 29, 1996