<< Chapter < Page Chapter >> Page >

Sun explains it this way:

"The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch."

As of JDK 1.4.0, there are more than fifty known subclasses of the Exception class. Many of these subclasses themselves have numerous subclasses, so there is quite a lot of material that you need to become familiarwith.

The RuntimeException class

One subclass of Exception is the class named RuntimeException As of JDK 1.4.0, this class has about 30 subclasses, many which are furthersubclassed. The class named RuntimeException is a very important class.

Unchecked exceptions

The RuntimeException class, and its subclasses, are important not so much for what they do, but for what they don't do. I will refer to exceptionsinstantiated from RuntimeException and its subclasses as unchecked exceptions.

Basically, an unchecked exception is a type of exception that you can optionally handle, or ignore. If you elect to ignore the possibility of anunchecked exception, and one occurs, your program will terminate as a result. If you elect to handle an unchecked exception and one occurs, the result willdepend on the code that you have written to handle the exception.

Checked exceptions

All exceptions instantiated from the Exception class, or from subclasses of Exception other than RuntimeException and its subclasses must either be:

  • Handled with a try block followed by a catch block, or
  • Declared in a throws clause of any method that can throw them

In other words, checked exceptions cannot be ignored when you write the code in your methods. According to Flanagan, the exception classes in thiscategory represent routine abnormal conditions that should be anticipated and caught to prevent program termination.

Checked by the compiler

Your code must anticipate and either handle or declare checked exceptions. Otherwise, your program won't compile. (These are exception types that are checked by the compiler.)

Throwable constructors and methods

As mentioned above, all errors and exceptions are subclasses of the Throwable class. As of JDK 1.4.0, the Throwable class provides four constructors and about a dozen methods. The four constructors are shown in Figure 1 .

Figure 1 . Throwable constructors.
Throwable() Throwable(String message)Throwable(String message,Throwable cause) Throwable(Throwable cause)

The first two constructors have been in Java for a very long time. Basically, these two constructors allow you to construct an exception objectwith, or without a String message encapsulated in the object.

New to JDK 1.4

The last two constructors are new in JDK 1.4.0. These two constructors are provided to support the cause facility. The cause facility is new in release 1.4. It is also known as the chained exception facility. (I won't cover this facility in this module. Rather, I plan to cover it in a series of future modules.)

Methods of the Throwable class

Figure 2 shows some of the methods of the Throwable class. (I omitted some of the methods introduced in JDK 1.4 for the reasons given above.)

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




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

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask