This page is optimized for mobile devices, if you would prefer the desktop version just click here

2.15 Java1630: exception handling  (Page 2/17)

Exceptions in code written by others

There are also situations where you don't write the code to throw the exception object, but an exceptional condition that occurs in code written bysomeone else transfers control to exception-handling code that you write.

For example, the read method of the InputStream class throws an exception of type IOException if an exception occurs while the read method is executing. In this case, you are responsible only for the code in the catch block and optionally for the code in the finally block.

(This is the reason that you must surround the call to System.in.read() with a try block followed by a catch block, or optionally declare that your method throws an exception of type IOException .)

Exception hierarchy, an overview

When an exceptional condition causes an exception to be thrown , that exception is represented by an object instantiated from the class named Throwable or one of its subclasses.

Here is part of what Sun has to say about the Throwable class:

"The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one ofits subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause."

Sun goes on to say:

"Instances of two subclasses, Error and Exception , are conventionally used to indicate that exceptionalsituations have occurred. Typically, these instances are freshly created in the context of the exceptionalsituation so as to include relevant information (such as stack trace data)."

The Error and Exception classes

The virtual machine and many different methods in many different classes throw exceptions and errors . I will have quite a lot more to say about the classes named Error and Exception later in this module.

Defining your own exception types

You may have concluded from the Sun quotation given above that you can define and throw exception objects of your own design, and if you did, that is a correct conclusion. (Your new class must extend Throwable or one of its subclasses.)

The difference between Error and Exception

As mentioned above, the Throwable class has two subclasses:

  • Error
  • Exception

What is an error?

What is the difference between an Error and an Exception ? Paraphrasing David Flanagan and his excellent series of books entitled Javain a Nutshell, an Error indicates that a non-recoverable error has occurred that should not be caught. Errors usually cause the Java virtualmachine to display a message and exit.

Sun says the same thing in a slightly different way:

"An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errorsare abnormal conditions."

For example, one of the subclasses of Error is named VirtualMachineError . This error is "Thrown to indicate that the Java Virtual Machine is broken or has run out of resources necessary for it tocontinue operating. "

What is an exception?

Paraphrasing Flanagan again, an Exception indicates an abnormal condition that must be properly handled to prevent program termination.

<< Chapter < Page Page > Chapter >>

Read also:

OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.