Any Method on the Chain can Catch

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
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?