Java——標準異常

來源:互聯網
上載者:User

標籤:owa   需要   表示   col   row   java   thread   ring   rgs   

Throwable這個java類被用來表示任何可以作為異常被拋出的類,Throwable可以分為兩種類型,Error用來表示編譯時間和系統錯誤,Exception是可以被拋出的基本類型。

1、RuntimeException

public class Tree1 {        public static void f() {        throw new RuntimeException();    }        public static void g(){            f();    }        public static void main (String[] args){            g();    }}

輸出結果:

Exception in thread "main" java.lang.RuntimeException    at Tree1.f(Tree1.java:6)    at Tree1.g(Tree1.java:10)    at Tree1.main(Tree1.java:14)

一般的異常都需要這樣處理,

       try{            ///可能會拋出異常的代碼        }        catch(Type1 id1){            //處理Type1類型異常的代碼        }        catch(Type2 id2){            ///處理type2類型異常的代碼        }

RuntimeException就不是一般的異常,屬於運行時異常,這種異常會自動被java虛擬機器拋出,將會被自動捕獲。

但是:只能在代碼中忽略RuntimeException(及其子類)類型的異常,其他類型的異常的處理都是由編譯器強制實施的,因為其實RuntimeException代表的就是編程錯誤,

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.