Adapted from https://docs.oracle.com/javase/tutorial/essential/exceptions/throwing.html
The
throw
statement requires a single argument: a throwable object. Throwable objects are instances of any subclass of the Throwable
class. Here's an example of a throw
statement.throw someThrowableObject; // Note: here is an object
Throwable Class and Its Subclasses
The objects that inherit from theThrowable
class include direct descendants (objects that inherit directly from theThrowable
class) and indirect descendants (objects that inherit from children or grandchildren of theThrowable
class). The figure below illustrates the class hierarchy of theThrowable
class and its most significant subclasses. As you can see,Throwable
has two direct descendants:Error
andException
. The Throwable class.Error Class
When a dynamic linking failure or other hard failure in the Java virtual machine occurs, the virtual machine throws anError
. Simple programs typically do not catch or throwError
s.Exception Class
AnException
indicates that a problem occurred, but it is not a serious system problem. Most programs you write will throw and catchException
s as opposed toError
s.The Java platform defines the many descendants of theException
class. These descendants indicate various types of exceptions that can occur. For example,IllegalAccessException
signals that a particular method could not be found, andNegativeArraySizeException
indicates that a program attempted to create an array with a negative size.OneException
subclass,RuntimeException
, is reserved for exceptions that indicate incorrect use of an API. An example of a runtime exception isNullPointerException
, which occurs when a method tries to access a member of an object through anull
reference.
A program can use exceptions to indicate that an error occurred. To throw an exception, use thethrow
statement and provide it with an exception object — a descendant ofThrowable
— to provide information about the specific error that occurred. A method that throws an uncaught, checked exception must include athrows
clause in its declaration.A program can catch exceptions by using a combination of thetry
,catch
, andfinally
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 thetry
block.Thetry
statement should contain at least onecatch
block or afinally
block and may have multiplecatch
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.
No comments:
Post a Comment