(Review: ) What must your code do with a checked exception?

A good answer might be:

  1. Handle it in a catch{} block, or
  2. throw it to the method that called this one.

Passing the Buck

If a method throws a checked exception back to its caller, the caller must do one of the same two things. And if the caller throws the exception to its caller, then its caller must do the same two things, and so on. Ultimately the exception will be handled, possibly by the runtime system (which terminates the program and prints the stack trace.) Here is an outline of some code:

public class Passing
{

  public static void methodC() throws IOException
  {
     // Some I/O statements
  }

  public static void methodB() throws IOException
  {
     methodC();
  }

  public static void methodA() throws IOException
  {
     methodB();
  }

  public static void main ( String[] a ) throws IOException
  {
     methodA();
  }

}

Every method in this example throws IOExceptions back to its caller. Since it is the Java run time system that called main(), the exception will finally reach the run time system, which will terminate the program and print a stack trace.

QUESTION 7:

If an IOException occured in methodC(), what would the stack trace look like (roughly)?

Click Here after you have answered the question