The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.
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.
A program can catch exceptions by using a combination of the try
, catch
, and finally
blocks.
try
block identifies a block of code in which an exception can occur.catch
block identifies a block of code, known as an exception handler, that can handle a particular type of exception.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.The try
statement should contain at least one catch
block or a finally
block and may have multiple catch
blocks.
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.