亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

哪位大哥能幫我看下 為什么編譯器為一直報錯!

public class HelloWorld{

? ? ?public static void main(String []args){

? ? ?

? ? ?HelloWorld hello=new HelloWorld();

? ?

? ? ?try{

? ? ?hello.test2();

? ? ?}

? ? ?catch(Exception r){

? ? ?r.printStackTrace();

? ? ?

? ? ?}}

? ? ?

? ? ?

? ? ?

? ? ?public void test1()throws Exception{

? ? throw new DrunkException("開酒別喝車");

? ? }

? ? ? ? ?public void test2(){

? ? ? ? try {

? ? test1();

? ? } catch (DrunkException e) {

? ? // TODO 自動生成的 catch 塊

? ? RuntimeException newrun=new RuntimeException("司機一滴酒,親人兩行淚");

? ? newrun.initCause(e);

? ? throw newrun;

? ? }

? ? ? ? ?

? ? ? ? ?} ?

? ? ?

? ? ?

}


在test2里面 會一直報錯 ?test1();需要添加拋出說明或者用try、catch包圍

明明已經包圍了啊??!


正在回答

3 回答

public void test1()throws Exception{ ??

這里改成public void test1()throws DrunkException{ ?就好了

但是我不知道為什么不能直接拋出基類。。。

0 回復 有任何疑惑可以回復我~

上面好像沒錯,你看看下面的代碼有沒有打錯?

public?class?DrunkException??extends?Exception{

????public?DrunkException(){

????????super();

????}

?????

????public?DrunkException(String?message){

????????super(message);//調用一下父類的有參構造方法

????}

? ?


0 回復 有任何疑惑可以回復我~

自定義的異常沒有寫吧

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

哪位大哥能幫我看下 為什么編譯器為一直報錯!

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號