close

Exception分類

●checked Exception

     --Exception 的子類別,但不是Runtime Exception的子類別。

     --如FileNotFoundException

     --一定要try-catch,若不寫會出現unreported exception

●Unchecked Exception

     --RuntimeException

     --如OutOfMemoryError

     --沒有硬性規定

※ 一個try可以有多個catch,但是catch的順序要由小到大

※Exception Handing

●Catch 可能處理事項

     --發出訊息

     --重試

     --錯誤回覆(Rollback Transaction)

●至少加入

     --e.printStackTrace();

 


※若把FileNotFoundException與IOException互換會出現"exception FileNotFoundException has already been caught"的錯誤

        try {
            FileInputStream file = new FileInputStream("");
            java.io.FileInputStream fis = new java.io.FileInputStream("");
            fis.read();
        } catch (java.io.FileNotFoundException e) {        //精準,Exception也可以
            e.printStackTrace();
        }catch(java.io.IOException e){
            e.printStackTrace();
        }


Run >> Clean and Build Project >> copy: java -jar "C:\...\Test2.jar" >> cmd

        try {
            int a = Integer.parseInt(args[0]);
            System.out.println(100/a);
        } catch (java.lang.ArithmeticException e) {
            System.out.println("ArithmeticException");
            e.printStackTrace();
        }catch(java.lang.Exception e){
            System.out.println("Exception");
            e.printStackTrace();
        }

Input:  null                   Out:  ArithmeticException

Input:  0                      Out:  Exception

arrow
arrow
    文章標籤
    Exception java
    全站熱搜

    淺翔 發表在 痞客邦 留言(0) 人氣()