The Java Program: Try_Block.java

  1 class One_Exception extends Exception {
  2    int argument;
  3    public One_Exception (int i) { argument=i; }
  4 }
  5 
  6 class Another_Exception extends Exception {}
  7 
  8 public class Try_Block {
  9 
 10    public static void main (String argv[]) {
 11 
 12       // Java "try" block with "catch" and "finally"
 13 
 14       try {
 15 
 16         // block of statements; may raise exceptions,
 17         // "break", "continue", or return.
 18 
 19          if (1==0) {
 20             throw new One_Exception (5);
 21          } else {
 22             throw new Another_Exception ();
 23          }
 24 
 25       } catch (One_Exception e) {
 26          // one handler
 27          System.out.println (e.argument);
 28 
 29       } catch (Another_Exception e) {
 30          // another handler
 31          e.printStackTrace (System.err);
 32 
 33       } finally {
 34          // final wishes; always executed no matter whether
 35          // we leave the block normally, with an exception,
 36          // because of a "break", "continue", or return
 37       }
 38    }
 39 }