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