拋出異常-throws和throw


package com.mpp.test;

import java.util.Scanner;

public class TryDemoFour {
    public static void main(String[] args) {
        try {
            testAge();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * throw拋出異常對象的處理方案
     * 1. 通過try.catch包含throw的語句--自己拋出自己處理
     * 2. 通過throws在方法聲明處拋出異常類型--誰用誰處理--調用者可以自己處理,也可以繼續向上拋
     * 3. 此時可以拋出與throw相同類型或者其父類
     */
    //描述酒店的入住規則:限定年齡,18歲以下,80歲以上的住客必須由親友陪同

    /*
    public static void testAge() {
        try {
            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("歡迎入住本酒店");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
*/

    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("歡迎入住本酒店");
            }
    }
}

 

throws:如果一個方法可能會出現異常,但沒有能力處理這種異常,可以在方法聲明處用throws子句來拋出異常類型。
throws 后面可以跟多個異常類型,用逗號分隔

 


當方法OAO出異常時,方法不對異常做處理,而是調用該方法處做異常處理

package com.mpp.test;

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

public class TryDemoThree {
    public static void main(String[] args) {
        /*
        try {
            int res = test();
            System.out.println("one和two的商是:" + res);
        }
        catch (ArithmeticException e){
            System.out.println("除數不允許為零");
            e.printStackTrace();
        }
        catch (InputMismatchException e){
            System.out.println("不支持非數字");
            e.printStackTrace();
        }
        */

        try{
            int res = test();
            System.out.println("one和two的商是:" + res);
        }
        catch (ArithmeticException e){

        }
        catch (InputMismatchException e){

        }
        catch (Exception e){

        }
//        test();  //只拋出父類Exception時這里報錯
    }

    /*
    通過throws拋出異常時,針對可能出現的多種情況,解決方案:
    1. throws后面接多個異常類型,中間用逗號分隔
     */

    /*throws拋出異常,誰調用這個方法誰處理異常
    public static int test() throws ArithmeticException,InputMismatchException {
        Scanner input = new Scanner(System.in);
        System.out.println("=========運算開始=======");
            System.out.print("請輸入第一個整數:");
            int one = input.nextInt();
            System.out.print("請輸入第二個整數:");
            int two = input.nextInt();
            System.out.println("=========運算結束=======");
            return one / two;
       }
       */

    public static int test() throws Exception{
        Scanner input = new Scanner(System.in);
        System.out.println("=========運算開始=======");
        System.out.print("請輸入第一個整數:");
        int one = input.nextInt();
        System.out.print("請輸入第二個整數:");
        int two = input.nextInt();
        System.out.println("=========運算結束=======");
        return one / two;
    }
}

throw:拋出異常對象,拋出的只能是可拋出類Throwable或者其子類的實例對象

 

 有兩種處理方法

一種是拋出異常類型對象,自己的方法進行處理異常

 

一種是拋出異常,調用該方法處進行異常處理

 

 

 

 
       


免責聲明!

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



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