淺談Java中異常

來源:互聯網
上載者:User

標籤:void   導致   使用   編碼   catch   main   finally   static   exception   

RuntimeException和checked Exception的區別:

1.錯誤分為兩種:throwable 兩種Errow和 Exception

Exception由編碼導致的錯誤:

e.g:

int b=0;
int a=5/b;
system.out.println(a);

2.try catch finally捕獲異常

把可能出現異常的代碼,放入try中

catch(exception e){}

finally 是最終的意思,不管怎麼樣都會執行的

e.g:

public static void main(String[] args) {//當輸入不正確的時候

do{
Scanner input=new Scanner(System.in);
System.out.println("請輸入一個數:");
try{
int a=input.nextInt();
System.out.println("你輸入的是正確的"+a);
break;
}catch(Exception e){//錯誤類型的時候運行
System.out.println("輸入錯誤,請重新輸入");


}
finally {
System.out.println("1");

}
}while(true);

}

3.throw 和thows區別

throw 拋出的是對象 throw a;( Exception a=new Exception)

throws 拋出的是聲明e.g throws Exception


4.檢查異常,使用的時候要求加上try catch的異常

5.異常重寫

重寫拋出的異常比父類更小的異常或者預設和父類相同

在主方法執行的時候依然要遵循異常的處理

 

以上都是些小知識,***

淺談Java中異常

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.