The Java Program: Recurse2.java

  1 public class Recurse2 {
  2 
  3    public static void q (int n) throws Exception {
  4       class Zero extends Exception {};
  5       try {
  6          if (n>0) p(n-1); else throw new Zero ();
  7       } catch (Zero e) {
  8          System.out.println ("q: Exception 'Zero'");
  9          throw e;
 10       } catch (Exception e) {
 11          System.out.println ("q: Some other exception");
 12          throw e;
 13       }
 14    }
 15 
 16    public static void p (int n) throws Exception {
 17       class Zero extends Exception {};
 18       try {
 19          if (n>0) q(n-1); else throw new Zero ();
 20       } catch (Zero e) {
 21          System.out.println ("p: Exception 'Zero'");
 22          throw e;
 23       } catch (Exception e) {
 24          System.out.println ("p: Some other exception");
 25          throw e;
 26       }
 27    }
 28     
 29    public static void main (String argv[]) {
 30       try {
 31          p (5);
 32       } catch (Exception e) {
 33          System.out.println ("Giving up");
 34       }
 35       try {
 36          q (5);
 37       } catch (Exception e) {
 38          System.out.println ("Giving up");
 39       }
 40    }
 41 }