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

2.15 Java1630: exception handling  (Page 4/17)

Figure 2 . Methods of the Throwable class.
fillInStackTrace() getStackTrace()printStackTrace(). setStackTrace(StackTraceElement[]stackTrace) getLocalizedMessage()getMessage() toString()

The StackTrace

The first four methods in Figure 2 deal with the StackTrace . In case you are unfamiliar with the term StackTrace, this is a list of the methods executed in sequence that led to the exception. (This is what you typically see on the screen when your program aborts with a runtime error that hasn't been handled.)

Messages

The two methods dealing with messages provide access to a String message that may be encapsulated in the exception object. The getMessage class simply returns the message that was encapsulated when the object wasinstantiated. (If no message was encapsulated, this method returns null.)

The getLocalizedMessage method is a little more complicated to use. According to Sun, "Subclasses may override this method in order to produce a locale-specific message."

The toString method

The toString method is inherited from the Object class and overridden in the exception subclass to "return a short description of the Throwable ".

Inherited methods

All exception objects inherit the methods of the Throwable class, which are listed in Figure 2 . Thus, any of these methods may be called by the code in the catch block in its attempt to successfully handle the exception.

For example, exceptions may have a message encapsulated in the exception object, which can be accessed using the getMessage method. You can use this to display a message describing the error or exception.

You can also use other methods of the Throwable class to:

  • Display a stack trace showing where the exception or error occurred
  • Produce a String representation of the exception object

So, what is an exception?

According to the online book entitled The Java Tutorial by Campione and Walrath:

"The term exception is shorthand for the phrase "exceptional event". It can be defined as follows:

Definition: An exception is an event that occurs during the execution of a program that disrupts the normal flow ofinstructions."

When an exceptional condition occurs within a method, the method may instantiate an exception object and hand it off to the runtime system to dealwith it. This is accomplished using the throw keyword. (This is called throwing an exception.)

To be useful, the exception object should probably contain information about the exception, including its type and the state of the program when theexception occurred.

Handling the exception

At that point, the runtime system becomes responsible for finding a block of code designed to handle the exception.

The runtime system begins its search with the method in which the exception occurred and searches backwards through the call stack until it finds a methodthat contains an appropriate exception handler (catch block).

An exception handler is appropriate if the type of the exception thrown is the same as the type of exception handled by the handler, or is asubclass of the type of exception handled by the handler.

<< 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.