Home Java Exceptions Exception Classes

 
 

Share on Google+Share on Google+

Exception Classes

Advertisement
The hierarchy of exception classes commence from Throwable class which is the base class in java.lang. This class can be instantiated and thrown by the program.

Exception Classes

     

The hierarchy of exception classes commence from Throwable class which is the base class for an entire family of exception classes, declared in  java.lang package as java.lang.Throwable. A throwable contains a snapshot of the execution stack at the time it was created and also a message string that gives more information about the error. This class can be instantiated and thrown by the program. The throwable class is further divided into two subclasses :- 

  1. Exceptions - Exceptions are thrown if any kind of unusual condition occurs that can be caught. Sometimes it also happens that the exception could not be caught and the program may get terminated. Remember that they are a member of Exception family and can be type of Checked or Unchecked exception.
  2. Errors - When any kind of serious problem occurs which could not be handled easily like OutOfMemoryError then an error is thrown. Well, errors are not something which is thrown by you rather they are thrown by the Java API or by the Java virtual machine itself i.e. only the exceptions are thrown by your code and not the errors. Also Remember that they are a member of Error family.

The exception classes can be explained as well seeing the exception hierarchy structure:

 

 

The java.lang package defines several classes and exceptions. Some of these classes are not checked while some other classes are checked.

EXCEPTIONS DESCRIPTION CHECKED UNCHECKED
ArithmeticException Arithmetic errors such as a divide by zero

-

YES
ArrayIndexOutOfBoundsException Arrays index is not within array.length - YES
ClassNotFoundException Related Class not found YES -
IOException InputOuput field not found YES -
IllegalArgumentException Illegal argument when calling a method - YES
InterruptedException One thread has been interrupted by another thread YES -
NoSuchMethodException Nonexistent method YES -
NullPointerException Invalid use of null reference - YES
NumberFormatException Invalid string for conversion to number - YES

As you have come to know that exceptions are Objects that means an object is thrown when you throw an exception. Moreover only those objects could be thrown whose classes are derived from Throwable

It is interesting to note here that the objects of your own design could also be thrown provided that they should be the subclass of some member of the Throwable family. Also the throwable classes which are defined by you must extend Exception class. 

It depends upon the situation that whether to use an existing exception class from java.lang or create any of your own. Such as IllegalArgumentException, a subclass of RuntimeException in java.lang can be thrown if any method with an invalid argument is thrown by you. On the other hand you need not to worry if you wish to impart some more information about any unusual condition other than a class from java.lang because it will be indicated by the class of exception object itself.  

For example, if a thrown exception object has class IllegalArgumentException, that indicates someone passed an illegal argument to a method. Sometimes you will want to indicate that a method encountered an abnormal condition that isn't represented by a class in the Throwable family of java.lang.
For instance, lets tweak an example below that demonstrates the exceptional conditions that might occur while driving a car.

// In Source Packet in file except/ex1/SpeedException.java
class SpeedException extends Exception {
}
// In Source Packet in file except/ex1/VeryFastException.java
class VeryFastException extends SpeedException {
}
// In Source Packet in file except/ex1/VerySlowException.java
class VerySlowException extends SpeedException {
}

Lets tweak the diagram below.

It is clear from the above program that there is something abnormal with the speed of the car i.e. either it is very fast or it is very slow. Hence two exceptions are thrown by the program - VeryFastException and VerySlowException. To be more precise the SpeedException family specifies three new exceptions thrown by the program which indicate some abnormal conditions. That is the SpeedException specifies that there is something unusual with the speed; VeryFastException and VerySlowException specifies the abnormal conditions of the speed.

NOTE: The SpeedException extends Exception only and not the Throwable or Error class.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 13, 2007

Related Tutorials

Discuss: Exception Classes   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:7
Chandana
June 23, 2011
RuntimeException is a sub class of Exception

RuntimeException Extends from Exception not from Throwable..
Chandana
June 23, 2011
RuntimeException is a sub class of Exception

RuntimeException Extends from Exception not from Throwable..
athar
July 28, 2011
Error in the diagram

Runtime exception is subclass of Exception class.
Jai
November 14, 2011
Fw: Re:

this is wrong statement
satish kumar
June 20, 2012
java

the explaniation are easy to understand
amit kumar jha
June 27, 2012
java

nice tutorial
Jaikrat
July 18, 2012
First diagram is wrong

I think the first diagram is wrong. The RuntimeException come out of the Exception not directly from Throwable.
DMCA.com