JAVA常用工具類異常處理


1異常的定義

異常就是與我們編譯相違背在過程中出現的邏輯或忘記一些賦值等等

分為編譯時錯誤和運行時錯誤

運行時異常

 我們一般處理的時Exception異常;

異常處理

異常處理可以通過關鍵字try,catch,finally,throw,throws;

try,catch,finally處理 捕獲異常

try捕獲異常,通常把try放在我們覺得出錯的位置;

catch 捕獲異常,當異常出現時,被catch捕獲通過Exception方法來提示異常(通常從異常最后一行開始處理異常);

finally

 

無論前面出現什么都會直接下面語句;

package com.jiedada.exception;

import java.util.Scanner;

public class Frist {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
   int x=0,y=0;
      
      System.out.println("=============運行開始=========");
      Scanner sc=new Scanner(System.in);
      try {
      System.out.println("請輸入分子");
       x=sc.nextInt();
      System.out.println("請輸入分母");
       y=sc.nextInt();
      }
      catch(Exception e)
      {
      System.out.println("================程序結束=========");
      e.printStackTrace();
      }
      finally {
      System.out.println("該算術表達式結果為"+(x/y));
      }
    }

}

 

 

catch可以使用多個子類的方法來判斷屬於那種類型的錯誤如ArithmeticException(算術表達式的錯誤)

在多個cat表達式中最后一個catch中最好或者一定要包含一個Exception的子方法表達式這樣才能檢查所有錯誤;

InputMismatchException(判斷輸入的類型不是不錯的

package com.jiedada.exception;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Frist {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
   int x=0,y=0;
      
      System.out.println("=============運行開始=========");
      Scanner sc=new Scanner(System.in);
      try {
      System.out.println("請輸入分子");
       x=sc.nextInt();
      System.out.println("請輸入分母");
       y=sc.nextInt();
       System.out.println("該算術表達式結果為"+(x/y));
      }
      catch(ArithmeticException e)
      {
          System.out.println("除數不能為0,請重新輸入");
          e.printStackTrace();
      }
      catch(InputMismatchException e)
      {
          System.out.println("請輸入整數");
          e.printStackTrace();
      }
      catch(Exception e)
      {
      System.out.println("出錯啦---");
      e.printStackTrace();
      }
      finally {
          System.out.println("================程序結束=========");
      }
    }

}
View Code

Sysout.exit(非0數)終止程序運行

return在這個結構中的使用:因為finally的存在,不管怎么都會執行finally中的語句所以

return寫在finally中都只會返回finally中的return;

 

throws聲明異常

是拋出異常,通過在方法后面添加throws和 錯誤信息提示如Exception,ArithmeticException;

是通過調用該方法的實列通過try catch finally來執行

可以通過文檔注釋對異常進行說明;

package com.jiedada.exception;

import java.util.Scanner;

public class Three {
    

    public static  void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            int result=test();
            System.out.println(result);
        } catch (ArithmeticException e) {
            // TODO Auto-generated catch block
            System.out.println("除數不能為0");
            e.printStackTrace();
        }
    }
    /**
     * 該方法是除法的運算
     * @return 該算術的商
     * @throws ArithmeticException
     */
    public static int test()throws ArithmeticException{
       int x=0,y=0;
       System.out.println("=============運行開始=========");
       Scanner sc=new Scanner(System.in);
      
       System.out.println("請輸入分子");
       x=sc.nextInt();
       System.out.println("請輸入分母");
       y=sc.nextInt();
          System.out.println("================程序結束=========");
          return x/y;
      
      }

}
View Code

 

throw處理拋出異常;

第一種方法:throw也是一種拋出異常結構為通過try catch在try中寫入throw new 異常類型();

第二種方法:和throws差不太多,通過在代碼中寫入throw new 異常類型()[建議不要拋出非檢查異常如,ArithmeticException]

通過在方法后面添加throws和 錯誤信息提示如Exception;誰調用誰就通過try catch異常處理;

在Exception中有輸入字符串的方法如下:

在第二種中throw new 異常類型()通過在方法后面添加throws后面的異常處理必須要比throw中的大,如

 

package com.jiedada.exception;

import java.util.Scanner;

public class Four {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            testAge();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
 /*public static void testAge()
 {
     
     System.out.println("請輸入入住者的年齡");
     Scanner input=new Scanner(System.in);
     int age=input.nextInt();
     if(age<18||age>80)
     {
         try {
            throw new Exception("18歲以下,80歲以上的人需要家人的陪同入住");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     }else
     {
         System.out.println("歡迎入住");
     }*/
     
    public static void testAge() throws Exception
     {
         
         System.out.println("請輸入入住者的年齡");
         Scanner input=new Scanner(System.in);
         int age=input.nextInt();
         if(age<18||age>80)
         {
             
                throw new Exception("18歲以下,80歲以上的人需要家人的陪同入住");
            
         }else
         {
             System.out.println("歡迎入住");
         }
}
}
View Code

 

 自定義異常

當需要反復使用一些異常時我們可以自己定義:先創建一個異常繼承Eception類,然后和Exception的方法相同。

在創建類的時候我們和其他的類一樣需要創建構造類;

 

 異常鏈

多個throw的向上拋出產生的異常問腿

 

package com.jiedada.exception;

public class Five {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            ThreeTest();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        

    }
   public static void OneTest() throws HotelException
   {
       throw new HotelException();
   }
   public static void TwoTest() throws Exception
   {
       try {
           OneTest();
       }catch(HotelException e)
       {
           throw new Exception("這是第三個TEST",e);
       }
   }
   public static void ThreeTest() throws Exception
   {
       try {
           TwoTest();
       }catch(Exception e2)
       {
          //throw new Exception("這是第三個TEST");
           Exception e1=new Exception();
           e1.initCause(e2);
           throw e1;
       }
   }
   
}
View Code

 

 

 

 總結

 

 

保證能夠正常執行當加入System.exit(非0數字)會終端;在該語句中return只會在執行了finally后才會返回值;

 當子類繼承父類的拋出方法是,子類只能拋出父類拋出方法的子類或者同類

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM