Technically Exception is a java class. To represent different kinds of exceptions in java we do have different java classes. The objects of these exception classes behave little different than other objects. Like only exception objects we could use to terminate the flow of execution. But we cannot user other objects to terminate the flow of execution.
So to differentiate these exception classes by compiler and JVM, Java has made all exception class hierarchy to extend from the Throwable
class. Only instances of Throwable
(or its subclass) can be thrown by JVM or throw
keyword. Only Throwable
(or its subclass) can be caught via catch
block. Only instances of Throwable
(or its subclass) can be declared with throws keyword.
Throwable
class has some common methods to deal with exceptions like getMessage()
indicating the error message, getCause()
to get the root cause of the exception, printStackTrace() to print the stack trace of an exception… etc. Below image represents Exception hierarchy in java.
