??
http://docs.oracle.com/javase/tutorial/essential/exceptions/definition.html
Throwable Class and Its Subclasses:
Java中方法的調用構成方法調用棧,方法調用棧中方法的順序與方法的被調用順序是相反的。
Java中程序的任何一處拋出Throwable對象后,都是采用按方法調用棧逐級上溯(即與方法調用順序相反的順序逐級上溯)的機制來搜尋可以處理被拋出的Throwable對象的方法( 注意不管是Checked Exceptions 還是 Unckecked Exceptions,都是使用這種機制處理的 )。如果找到了,則異常對象在該方法中被處理;如果搜尋所有方法直至程序入口都未能找到一個可以處理該Throwable對象的方法,則程序退出。如下面兩個圖所示:
Checked Exceptions & Unckecked Exceptions:
Checked Exceptions:
any subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses.
checked means they will be checked at compiletime it self,so Checked exceptions must be explicitly caught or propagated (只有兩條路:使用try-catch-finally捕獲它,或者在方法聲明中throws它)。
Unckecked Exceptions:
RuntimeException, Error, and their subclasses
unchecked means you need not handle the unchecked exceptions and they will be handled by the JVM.
什么時候使用checked exception,什么時候使用unchecked exception?
Here's the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.
對checked exception,什么時候應該拋出,什么時候應該捕獲?Catch or Propagate Exceptions?
You might be wondering whether you should catch or propate exceptions thrown in your program. It depends on the situation. In many applications you can't really do much about the exception but tell the user that the requested action failed. In these applications you can usually catch all or most exceptions centrally in one of the first methods in the call stack. You may still have to deal with the exception while propagating it though (using finally clauses). For instance, if an error occurs in the database connection in a web application, you may still have to close the database connection in a finally clause, even if you can't do anything else than tell the user that the action failed. How you end up handling exceptions also depends on whether you choose checked or unchecked exceptions for your application. There is more on that in other texts in the error handling trail.
throws和throw的區別
Sources:
Checked or Unchecked Exceptions?
http://tutorials.jenkov.com/java-exception-handling/checked-or-unchecked-exceptions.html
Basic try-catch-finally Exception Handling in Java
http://tutorials.jenkov.com/java-exception-handling/basic-try-catch-finally.html
http://docs.oracle.com/javase/tutorial/essential/exceptions/definition.html
Throwable Class and Its Subclasses:

Java中方法的調用構成方法調用棧,方法調用棧中方法的順序與方法的被調用順序是相反的。
Java中程序的任何一處拋出Throwable對象后,都是采用按方法調用棧逐級上溯(即與方法調用順序相反的順序逐級上溯)的機制來搜尋可以處理被拋出的Throwable對象的方法( 注意不管是Checked Exceptions 還是 Unckecked Exceptions,都是使用這種機制處理的 )。如果找到了,則異常對象在該方法中被處理;如果搜尋所有方法直至程序入口都未能找到一個可以處理該Throwable對象的方法,則程序退出。如下面兩個圖所示:
Checked Exceptions & Unckecked Exceptions:
Checked Exceptions:
any subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses.
checked means they will be checked at compiletime it self,so Checked exceptions must be explicitly caught or propagated (只有兩條路:使用try-catch-finally捕獲它,或者在方法聲明中throws它)。
Unckecked Exceptions:
RuntimeException, Error, and their subclasses
unchecked means you need not handle the unchecked exceptions and they will be handled by the JVM.
什么時候使用checked exception,什么時候使用unchecked exception?
Here's the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.
對checked exception,什么時候應該拋出,什么時候應該捕獲?Catch or Propagate Exceptions?
You might be wondering whether you should catch or propate exceptions thrown in your program. It depends on the situation. In many applications you can't really do much about the exception but tell the user that the requested action failed. In these applications you can usually catch all or most exceptions centrally in one of the first methods in the call stack. You may still have to deal with the exception while propagating it though (using finally clauses). For instance, if an error occurs in the database connection in a web application, you may still have to close the database connection in a finally clause, even if you can't do anything else than tell the user that the action failed. How you end up handling exceptions also depends on whether you choose checked or unchecked exceptions for your application. There is more on that in other texts in the error handling trail.
throws和throw的區別
引用
throw用來在語句中拋出一個異常,而throws用在方法聲明中,表示這個方法會拋出某類異常。
throw語法:throw <異常對象>
在方法聲明中,添加throws子句表示該方法將拋出異常。
throws語法:[<修飾符>]<返回值類型><方法名>([<參數列表>])[throws<異常類>]
其中:異常類可以聲明多個,用逗號分割。
區別:
throws可以單獨使用;
而對throw:
如果語句中throw拋出的是checked exception,則該checked exception必須被 捕獲(try-catch[-finally])或者拋出(方法聲明中throws) ;所以可以認為, 使用throw拋出checked exception的時候throw不可以單獨使用,必須和try-catch[-finally] 或 throws配合使用 ;
如果語句中throw拋出的是unchecked exception(RuntimeException,Error,及他們的子類),unchecked exception是不需要你在代碼中做處理的;可以認為, 使用throw拋出unchecked exception的時候throw可以單獨使用 。
在方法的一個分支中(主干分支或任意一個條件分支,條件分支如if分支、else分支),throw一個Throwable對象,則如果該分支中throw子句后還存在代碼塊,則這些代碼塊會被認為是Unreachable,無法編譯通過。
throw語法:throw <異常對象>
在方法聲明中,添加throws子句表示該方法將拋出異常。
throws語法:[<修飾符>]<返回值類型><方法名>([<參數列表>])[throws<異常類>]
其中:異常類可以聲明多個,用逗號分割。
區別:
throws可以單獨使用;
而對throw:
如果語句中throw拋出的是checked exception,則該checked exception必須被 捕獲(try-catch[-finally])或者拋出(方法聲明中throws) ;所以可以認為, 使用throw拋出checked exception的時候throw不可以單獨使用,必須和try-catch[-finally] 或 throws配合使用 ;
如果語句中throw拋出的是unchecked exception(RuntimeException,Error,及他們的子類),unchecked exception是不需要你在代碼中做處理的;可以認為, 使用throw拋出unchecked exception的時候throw可以單獨使用 。
//存在三個分支:main主分支、if分支、else分支 public static void main( String[] args ) throws Throwable { if(true) { throw new Throwable("err"); //System.out.println("ddd"); //不可達 } else { System.out.println(""); //可達 } throw new NullPointerException("null err"); //可達 //m1(); //不可達 }
Sources:
Checked or Unchecked Exceptions?
http://tutorials.jenkov.com/java-exception-handling/checked-or-unchecked-exceptions.html
Basic try-catch-finally Exception Handling in Java
http://tutorials.jenkov.com/java-exception-handling/basic-try-catch-finally.html
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
