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

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

自定義異常的使用

package ImoocException;


public class my_Exception extends Exception {

/**

*?

*/

private static final long serialVersionUID = 1L;

public my_Exception() {

System.out.println("使用了這個自定義的類1!");

}

public my_Exception(String str) {

super(str);

System.out.println("使用了這個自定義的類2!");

}

public void devide(int a,int b) throws Exception {

if(b==0) {

throw new my_Exception("被除數不能為0");

}

else {

System.out.println("it is ok!");

}

}

public static void main(String[] args)throws Exception {

// TODO Auto-generated method stub

ImoocException ?imooc=new ImoocException ();

try {

imooc.devide(10, 0);

}

catch(my_Exception e) {

? ?System.out.println("異常拋出!");

}

}


}


最后結果還是出來

Exception in thread "main" java.lang.Exception

at ImoocException.ImoocException.devide(ImoocException.java:6)

at ImoocException.my_Exception.main(my_Exception.java:29)

這是怎么回事?

正在回答

2 回答

public static void main(String[] args)throws Exception 這里拋出異常??

mian方法是程序的起點? 在拋出只能交給JVM處理? 相當于自己沒有捕獲異常? 沒有寫try---catch

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

public static void main(String[] args)throws Exception

這里不需要拋異常了吧

public void devide(int a,int b) throws Exception 是不是要改成這樣

public void devide(int a,int b) throws my_Exception


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

舉報

0/150
提交
取消
Java入門第三季
  • 參與學習       409767    人
  • 解答問題       4543    個

Java中你必須懂得常用技能,不容錯過的精彩,快來加入吧

進入課程

自定義異常的使用

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

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

幫助反饋 APP下載

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

公眾號

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