Hierarchy of Exceptions (Checked Unchecked Exceptions)
After knowing exceptions and exception handling mechanism, let us explore how many exceptions exist, are they inheriting from a common super class, any classification exists or not etc. Whenever you come across a new Java class, it is essential to know its super classes, so that, you can use their methods also, if necessary in the code; a true exploitation of inheritance principle. Previously, you came across two new exception classes – ArithmeticException and ArrayIndexOutOfBoundsException. Now let us see where from they are coming. Following hierarchy gives their address.
As you can guess from the above hierarchy, the super class of all exceptions is Throwable. It includes two subclasses Error and Exception. Observe, every class in the hierarchy suffixes with Exception, but Error does not. It looks as an odd man. It is really an odd man. All the exceptions are caused by software problems but Error is caused by hardware problems, of course at runtime only. Generally programmers do not bother about the exceptions of type Error because they know pretty well, even if they handle, it is of no use. For example, we cannot create and save one more file when the hard disk is full. Java permits you, at runtime, to create new files, save the files, create new directories, rename the existing files and directories etc. through a program.
Error is different from other exceptions like stack over flow at run time. Error is an exception that happens at run time and depends on a particular situation. Error exception indicates a very serious problem that is usually unrecoverable and should never be caught.
The above hierarchy of exceptions can be broadly divided into two categories – unchecked exceptions and checked exceptions. The open nodes in the hierarchy say still some exceptions exist (say, in hundreds).
RuntimeException and its all subclasses including Error are known as unchecked exceptions. As the name indicates, even if they are not handled or checked by the programmer, the program simply compiles. If at runtime, problems arise, the execution simply terminates. That is, regarding unchecked exceptions, the compiler does not bother. Common examples are ArithmeticException, ArrayIndexOutOfBoundsException, NumberFormatException, NullPointerException etc.
Throwable and its subclasses except Error and RuntimeException put together are called as checked exceptions. If these exceptions are not handled or checked by the programmer in coding, the program does not compile. That is, compiler bothers much of this type of exceptions because they are raised in some special cases in the code which if not handled, the program may lead to many troubles. Common examples are FileNotFoundException, IOException and InterruptedException etc.
To say straight forward, he difference between unchecked and checked is just compilation. Java treats unchecked exceptions occur very commonly and checked exceptions very rarely in some special cases. If unchecked are also to be treated as checked, it may be necessary to keep every Java statement in a try-catch block which is very laborious and decreases the readability to a greater extent.
Exceptions – Java API support
The java.lang package comes with many classes that helps the programmer to handle the exceptions. Exception handling is one of the features of a modern programming language.
Pass your suggestions to improve the quality of this tutorial Checked Unchecked Exceptions.