Sử dụng Finally trong Java



Miêu tả vấn đề

Cách sử dụng khối finally để bắt exception trong Java?

Giải pháp

Ví dụ sau minh họa cách sử dụng khối finally để bắt runtime exception bởi sử dụng e.getMessage() trong Java.

public class ExceptionDemo2 {
   public static void main(String[] argv) {
      new ExceptionDemo2().doTheWork();
   }
   public void doTheWork() {
      Object o = null;
      for (int i=0; i<5; i++) {
         try {
            o = makeObj(i);
         }
         catch (IllegalArgumentException e) {
            System.err.println
            ("Error: ("+ e.getMessage()+").");
            return;   
         }
         finally {
            System.err.println("All done");
            if (o==null)
            System.exit(0);
        }
        System.out.println(o); 
      }
   }
   public Object makeObj(int type) 
   throws IllegalArgumentException {
      if (type == 1)  
      throw new IllegalArgumentException
      ("Don't like type " + type);
      return new Object();
   }
}

Kết quả

Code trên sẽ cho kết quả sau:

All done
java.lang.Object@1b90b39
Error: (Don't like type 1).
All done

exception_trong_java.jsp