Exception Hierarchy
What is an Exception
An exception is a problem that occurs during the execution of a program and disrupts the normal flow of the program.
Exception Hierarchy
All the checked exceptions and unchecked exceptions are subclasses of Exception class and the Exception class is subclass of Throwable class as shown below.
Types of Exceptions
There are two types of exceptions in java 1. Checked exceptions 2. Unchecked exceptions
1) Checked Exceptions: are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception(using try-catch block) or it must specify the exception using throws keyword. Else, the code will not compile.
2) Unchecked Exceptions: are the exceptions that are not checked at compiled time, so it is not forced by the compiler to either handle or specify the exception. It is up to the programmers to be civilized, and specify or catch the exceptions.
In Java exceptions under Error and RuntimeException classes are unchecked exceptions, everything else under throwable is checked.
In Java exceptions under Error and RuntimeException classes are unchecked exceptions, everything else under throwable is checked.
Example of checked Exception in Java API
IOException
SQLException
DataAccessException
ClassNotFoundException
InvocationTargetException
SQLException
DataAccessException
ClassNotFoundException
InvocationTargetException
Example of unchecked Exception in Java API
NullPointerException
ArrayIndexOutOfBound
IllegalArgumentException
IllegalStateException
ArrayIndexOutOfBound
IllegalArgumentException
IllegalStateException
That’s it for Exception Hierarchy in Java.
Useful links:
Comments
Post a Comment