第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

覆蓋 Java 中的異常

覆蓋 Java 中的異常

烙印99 2023-02-23 10:15:17
public class Student {    checkAge (Student student) {        try {            if (student.getAge() > 18 ) {                throw new CustomException("Student is older than 18 years.");            }        } catch (CustomException e) {            handleException(e);        }    } public class HandleException {    public static sendToErrorReport (CustomException customException) {        //trying to do something like this, but the below code throws an error.        customException.setMessage(customException.getMessage() +" ; Student -> " + customException.getStudent().getStudentName);    }}我已經(jīng)創(chuàng)建了一個自定義類來處理我的項(xiàng)目中發(fā)生的異常。我的要求是更改異常消息并將一些數(shù)據(jù)附加到異常消息并將其傳遞給我的自定義類。所以基本上我需要在設(shè)置異常消息后對其進(jìn)行編輯。有什么辦法可以做到這一點(diǎn)?
查看完整描述

3 回答

?
繁華開滿天機(jī)

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個贊

這是擴(kuò)展 RuntimeException 類的完整 CustomException 類。您可以定義代碼和消息。


public class CustomException extends RuntimeException {


    private String code;

    private String message;


    public CustomException() {

        super();

    }


    public CustomException(Exception e) {

        super(e);

    }


    public CustomException(String code, String message, Exception e) {

        super(message, e);

        this.code = code;

    }

}


查看完整回答
反對 回復(fù) 2023-02-23
?
一只名叫tom的貓

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超3個贊

你可以覆蓋類getMessage的方法Exception


class CustomException extends java.lang.Exception{


    @Override

    public String getMessage(){

        return super.getMessage() + "- My Message";

    }


}


查看完整回答
反對 回復(fù) 2023-02-23
?
倚天杖

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個贊

在您的代碼中拋出異常時


try{

//Some code here

}catch(Exception e){

 throw new CustomeException("Your text here")

}

當(dāng)您在其他地方捕獲 CustomeException 時,您可以對其進(jìn)行修改。


try{

//Some code here

}catch(CustomException e){

String message = e.getMessage();

//Do stuff with previous message

 throw new Custom2Exception("Your next text here")

}


查看完整回答
反對 回復(fù) 2023-02-23
  • 3 回答
  • 0 關(guān)注
  • 139 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號