Documentation

The Java™ Tutorials
Hide TOC
Summary小结
Trail: Essential Java Classes
Lesson: Exceptions

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.要抛出异常,请使用throw语句并为其提供异常对象—Throwable的后代—提供有关发生的特定错误的信息。A method that throws an uncaught, checked exception must include a throws clause in its declaration.抛出未捕获、已检查异常的方法必须在其声明中包含throws子句。

A program can catch exceptions by using a combination of the try, catch, and finally blocks.程序可以通过使用trycatchfinally块的组合来捕获异常。

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.通过异常连缀,异常可以指向导致它的异常,而异常又可以指向导致它的异常,依此类推。


Previous page: Advantages of Exceptions
Next page: Questions and Exercises