go to previous page   go to home page   go to next page

Answer:

The stack trace shows the chain of method calls that lead to the one that caused the exception, something like this:

java.lang.IOException:  some message
        at  some Java IO method
        at  BuckPassing.methodC
        at  BuckPassing.methodB
        at  BuckPassing.methodA
        at  BuckPassing.main 

In an actual stack trace, line numbers are listed for each method.


Any Method on the Chain can Catch

Any method in the chain of calling methods can catch the exception. The following is another way to organize the program:

public class BuckPassing
{

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

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

  public static void methodA()  
  {
     try
     {
       methodB();
     }
     catch ( IOException ex )
     {
       // statements to handle the exception
     }

  }

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

}

A method may handle some exceptions and throw others. Some of the exceptions it handles may originate in methods it calls, others may originate within itself. There may be as many try/catch/finally structures in a method as needed. The error handling logic of "industrial strength" programs can take up more lines of code than the "real" purpose of the program.

A method might throw several types of checked exceptions. Its throws clause looks like this:

throws ExceptionClass1, ExceptionClass2, ...

Listing unchecked exceptions in a throws clause is optional.


QUESTION 8:

(Memory Test: ) Is an ArithmeticException a checked exception?


go to previous page   go to home page   go to next page