實(shí)驗(yàn)十 異常處理

一、實(shí)驗(yàn)預(yù)熱
1、請(qǐng)解釋編譯時(shí)異常和運(yùn)行時(shí)異常的區(qū)別,并舉出幾個(gè)常見的編譯時(shí)異常和運(yùn)行時(shí)異常
1)編譯時(shí)異常是在編寫程序是就會(huì)發(fā)現(xiàn)的異常,例如:IOException、FileNotFoundException、SQLException、NoSuchMethodException、ClassNotFoundException
2)運(yùn)行時(shí)異常是程序在運(yùn)行期間發(fā)現(xiàn)的異常,例如:ArrayIndexOutOfBoundsException、ArithmeticException、IllegalArgumentException、ClassCastException、NumberFormatException、NullPointerException

2、請(qǐng)說出throw和throws的區(qū)別
Throw是拋出異常實(shí)例化對(duì)象(一定拋出異常)
Throws是拋出異常聲明(可能拋出異常)

二、實(shí)驗(yàn)內(nèi)容
1、自行完成一個(gè)簡單的自定義異常并處理,使程序正確執(zhí)行,并獲得異常返回信息。
代碼:

package leif.tests;

public class ExperimentalReport {
    public static void main(String[] args) {
        try {
            throw new ServiceException("ServiceException");
        } catch (ServiceException serviceException) {
            System.out.println(serviceException.getMessage());
            System.out.println(serviceException.getServiceExceptionMessage());
            serviceException.printStackTrace();
        }
    }
}

class ServiceException extends Exception {
    private static final long serialVersionUID = 1L;
    private String serviceExceptionMessage;

    public ServiceException(String serviceExceptionMessage) {
        super("Exception");
        this.serviceExceptionMessage = serviceExceptionMessage;
    }

    public String getServiceExceptionMessage() {
        return serviceExceptionMessage;
    }

    public void setServiceExceptionMessage(String serviceExceptionMessage) {
        this.serviceExceptionMessage = serviceExceptionMessage;
    }
}

結(jié)果截圖:


image.png

2、在程序中實(shí)現(xiàn)兩個(gè)數(shù)字求商的功能,當(dāng)除數(shù)為0時(shí),正確使用try-catch-finally捕捉如圖所示的程序異常。


image.png

代碼:

package leif.tests;

import java.util.Scanner;

public class ExperimentalReport {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("請(qǐng)輸入被除數(shù):");
        int dividend = scanner.nextInt();
        System.out.print("請(qǐng)輸入除數(shù):");
        int divisor  = scanner.nextInt();

        try {
            if (!isInterger(dividend) || !isInterger(divisor) || isZero(divisor)) {
                throw new ArithmeticException("出現(xiàn)錯(cuò)誤:被除數(shù)和除數(shù)必須是整數(shù),且除數(shù)不能為零。");
            }

            System.out.println(dividend / divisor);
        } catch (ArithmeticException arithmeticException) {
            arithmeticException.printStackTrace();
        } finally {
            scanner.close();
        }
    }

    public static boolean isInterger(Object object) {
        if (object instanceof Integer) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isZero(int i) {
        if (i == 0) {
            return true;
        } else {
            return false;
        }
    }
}

結(jié)果截圖:


image.png

3、在程序中編寫方法實(shí)現(xiàn)計(jì)算兩數(shù)的商,該方法使用throws聲明異常,在該方法的調(diào)用處使用try-catch-finally進(jìn)行異常捕捉。
代碼:

package leif.tests;

import java.util.Scanner;

public class ExperimentalReport {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("請(qǐng)輸入被除數(shù):");
        int dividend = scanner.nextInt();
        System.out.print("請(qǐng)輸入除數(shù):");
        int divisor  = scanner.nextInt();

        try {
            getResult(dividend, divisor);
        } catch (ArithmeticException arithmeticException) {
            arithmeticException.printStackTrace();
        } finally {
            scanner.close();
        }
    }

    public static int getResult(int dividend, int divisor) throws ArithmeticException {
        return dividend / divisor;
    }
}

結(jié)果截圖:


image.png

4、正確使用throw拋出異常,實(shí)現(xiàn)當(dāng)輸入的性別不是“男”或“女”時(shí),顯示如下圖所示的程序異常效果。


image.png

代碼:

package leif.tests;

import java.util.Scanner;

public class ExperimentalReport {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("請(qǐng)輸入性別:");
        String gender = scanner.next();

        try {
            if (!"男".equals(gender) && !"女".equals(gender)) {
                throw new Exception("性別必須是“男”或“女”!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            scanner.close();
        }
    }
}

結(jié)果截圖:


image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 【程序1】 題目:古典問題:有一對(duì)兔子,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子,小兔子長到第三個(gè)月后每個(gè)月又生一...
    阿里高級(jí)軟件架構(gòu)師閱讀 3,390評(píng)論 0 19
  • 【程序1】 題目:古典問題:有一對(duì)兔子,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子,小兔子長到第三個(gè)月后每個(gè)月又生一對(duì)兔...
    葉總韓閱讀 5,227評(píng)論 0 41
  • Java經(jīng)典問題算法大全 /*【程序1】 題目:古典問題:有一對(duì)兔子,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子,小兔子...
    趙宇_阿特奇閱讀 2,082評(píng)論 0 2
  • 【程序1】 題目:古典問題:有一對(duì)兔子,從出生后第3個(gè)月起每個(gè)月都生一對(duì)兔子,小兔子長到第三個(gè)月后每個(gè)月又生一對(duì)兔...
    開心的鑼鼓閱讀 3,395評(píng)論 0 9
  • 已經(jīng)記不清時(shí)隔幾年了沒動(dòng)筆了,零基礎(chǔ)的我還是選擇在今天重新拿起畫筆,只因?yàn)橄矚g。在這急躁時(shí)代里,想要自己靜下來,唯...
    醬西小丸子閱讀 1,742評(píng)論 0 4

友情鏈接更多精彩內(nèi)容