異常中的特殊情況--RuntimeException類

來源:互聯網
上載者:User

標籤:java   exception   runtimeexception   

如果在函數內容拋出該異常,函數上可以不用聲明,調用者可以不用進行處理,編譯一樣通過(當然,也可以聲明和處理)。之所以不用在函式宣告,是因為不需要讓調用者進行處理。當該異常發生時,希望程式停止。因為在運行時,出現了無法繼續運算的情況,希望停止程式後,對代碼進行修正。

自訂異常時,如果該異常的發生,無法再繼續進行運算,
就讓自訂異常繼承RuntimeException.

對於異常分兩種:
1,在編譯時間被檢測的異常。
函數內容拋,函數上拋,調用者要麼拋 要麼捕獲。
2,編譯時間不被檢測到的異常(運行時異常。RuntimeException以及其子類)
函數內容拋就可以了

class FushuException extends RuntimeException
{
FushuException(String msg)
{
super(msg);
}

}
class Demo
{
int div(int a,int,b)
if(b<0)
throw new FushuException("出現了除數為負數了");
}
class ExceptionDemo4
{
public static void main(String[] args)

{

Demo d = new Demo();
int x=div(4,-1);
System.out.println("x="+x);
System.out.println("over");

}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.