Lesson: Exceptions
The Java programming language uses exceptions to handle errors and other exceptional events. This lesson describes when and how to use exceptions.
java編程語言使用異常來處理錯誤和其它異常事件。這篇課程描述什麼時候和如何去使用異常。
What Is an Exception?
An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.
異常是一個發生在程序的執行期間的事件,它打斷了正常的指令流。
The Catch or Specify Requirement
This section covers how to catch and handle exceptions. The discussion includes the try, catch, and finally blocks, as well as chained exceptions and logging.
這節覆蓋瞭如何去捕獲和處理異常。這個論述包含了try
、catch
、finally blocks
以及鏈式異常和日誌。
How to Throw Exceptions
This section covers the throw statement and the Throwable class and its subclasses.
這節覆蓋了throw
語句、Throwable
類以及它的子類。
The try-with-resources Statement
This section describes the try-with-resources statement, which is a try statement that declares one or more resources. A resource is as an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement.
這節描述了try-with-resources
語句,它是一個try
語句聲明瞭一個或多個資源。資源是一個對象,在程序完成後必須關閉它。try-with-resources
語句確保每個資源在語句的結尾處將被關閉。
Unchecked Exceptions — The Controversy
This section explains the correct and incorrect use of the unchecked exceptions indicated by subclasses of RuntimeException.
這節解釋了未捕獲異常(RuntimeException
的子類)正確和錯誤的使用。
Advantages of Exceptions
The use of exceptions to manage errors has some advantages over traditional error-management techniques. You’ll learn more in this section.
與傳統的錯誤管理技術相比,使用異常去管理錯誤有一些優勢。你將會在這節中學到很多。