[an error occurred while processing this directive]

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.

ArithmeticException Arithmetic errors such as a divide by zero


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.


[an error occurred while processing this directive]