Summary

Summary

A program can use exceptions to indicate that an error occurred. To throw an exception, use the throw statement and provide it with an exception object — a descendant of Throwable — to provide information about the specific error that occurred. A method that throws an uncaught, checked exception must include a throws clause in its declaration.

程序可以使用異常去表示一個錯誤發生了。爲了拋出異常,使用throw子句並提供一個異常對象 - Throwable的子孫 - 提供關於發生的具體異常的信息。拋出一個未捕獲、受檢查異常的方法必須在它的聲明中包含throws子句。

A program can catch exceptions by using a combination of the try, catch, and finally blocks.

  • The try block identifies a block of code in which an exception can occur.
  • The catch block identifies a block of code, known as an exception handler, that can handle a particular type of exception.
  • The finally block identifies a block of code that is guaranteed to execute, and is the right place to close files, recover resources, and otherwise clean up after the code enclosed in the try block.

程序通過使用trycatchfinally語句塊,能捕獲異常。

  • try語句塊表示一個代碼塊中可能發生異常。
  • catch語句塊表示一個代碼塊,被稱爲異常處理器,它能處理一種特殊異常類型。
  • finally語句塊表示將被保證執行的一個代碼塊,是關閉文件、恢復資源、以及其它在try語句塊中的代碼之後執行清理的合適位置。

The try statement should contain at least one catch block or a finally block and may have multiple catch blocks.

try`語句必須包含至少一個catch語句塊,或是一個finally語句塊以及可能有多個catch`語句塊。

The class of the exception object indicates the type of exception thrown. The exception object can contain further information about the error, including an error message. With exception chaining, an exception can point to the exception that caused it, which can in turn point to the exception that caused it, and so on.

異常對象的類表示被拋出異常的類型。異常對象能夠包含更多關於錯誤的信息,包括錯誤消息。在異常鏈中,一個異常可以指向導致它的異常,被指向的異常也可以指向導致它的異常,依次類推。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章