Back to notes -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'  Slide program -- Keyboard shortcut: 't'    A Java program - main handles Problem.Lecture 9 - slide 27 : 30
Program 2
// Java
class Problem extends Exception{
  Problem(){super();}
  Problem(String s) {super(s);}
}

public class CatchOrSpecifyDemo1 {

  public static void main(String[] args){     
    try {
      explosion();
    }
    catch (Problem e) {
      System.out.println("We handled the problem!");
      System.out.println("Or did we?");
    }  // try
  }

  static void explosion() throws Problem{   
    System.out.println("Explosion!");
    throw new Problem("We have an explosive problem");
  }

}
 
 
 
 
 
 
 
 
Main catches Problem.
 
 
 
 
 
 
 
 
 
This method specifies Problem.