子父類中的異常

要點

Paste_Image.png

代碼

class AException extends Exception {
    
    AException(String msg) {
        super(msg);
    }
}

class BException extends AException {
    
    BException(String msg) {
        super(msg);
    }
}

class Fu {
    
    private int state = 2; //狀態(tài)值 1 :正常
     void method() throws AException {
        if (state == 2)
        throw new AException("A異常");
        System.out.println("fu method"); 
    }
}

class Text {
    
    void function(Fu f) {
        try{
            f.method();
        }
        catch(AException e) {
            System.out.println(e.toString());
        }
        System.out.println("OVER");
    }
}

class Zi extends Fu {
    
    private int state = 3; //狀態(tài)值 1 :正常
     void method() throws BException { //或者拋 AException 
        if (state == 3)
        throw new BException("B異常");
        System.out.println("zi method"); 
    }
}

class ExceptionDemo7 {
    
    public static void main(String[] args) {
        
        Text t = new Text();
        t.function(new Zi());
    
    }
}

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

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

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