TestThrow.java


import java.io.*;

public class TestThrow {
   public static void main (String[] args){
      for (int i = 0; i < 10; ++i) {
         try {
            try {
               if (i % 3 == 0)
                  throw new Exception("E0");
               System.out.println(i);
            }
            catch (Exception inner) {
               i *= 2;
               if (i % 3 == 0)
                  throw new Exception("E1");
            }
            finally {
               ++i;
            }
         }
         catch (Exception outer){
            i += 3;
         }
         finally {
            --i;
         }
      }
      
      try {
         BufferedReader d = new BufferedReader(new InputStreamReader(System.in)); 
         d.readLine();
      }
      catch (IOException ioe){
         System.out.println("More specific exception must be catch first.");
      }
      catch (Exception e) {
         System.out.println("Then we catch more general exception.");
      }
      finally{
         System.out.println("Finally must not be placed before catch block.");
         try {
            System.in.read();
         }
         catch (IOException e){
         }
      }
   }
}

// output, think why
// 4
// 5
//             (part of output, After user hits enter)
// Finally must not be placed before catch block.


Last updated: 12-10-2002
Copyright © 1999 - 2003 Roseanne Zhang, All Rights Reserved
[ Java certification page] [ SCJP FAQ] [ JavaChina]