1.3.3 改善后的异常处理

Demo:

import java.io.IOException;
import java.net.ServerSocket;

public class CoinTryCatch {
    
    public static void main(String[] args) {
        
        /** 本Demo有待优化得更合情合理且全面 **/
        try {
            // java.lang.ArithmeticException: / by zero
            System.out.println(5/0);
            // IO
            ServerSocket ss = new ServerSocket(8888);
            ss.close();
            // OutOf
            String[] sarr = new String[]{};
            System.out.println(sarr[0]);
        /** | **/
        } catch (IOException|ArrayIndexOutOfBoundsException e) {
            System.out.println("捕捉到IO或OutOf异常!");
        /** final Java 7 异常重抛——final关键字不是必须 **/
        } catch (final Exception e) {
            e.printStackTrace();
        }
        
    }

}

Ran As Java Application:

java.lang.ArithmeticException: / by zero
    at cointest.CoinTryCatch.main(CoinTryCatch.java:13)

 

posted @ 2016-01-05 12:22  springup  阅读(150)  评论(0)    收藏  举报