Throwable is a class of Java.lang package which is
considered as the superclass of all errors and exceptions in the Java language.
It should be kept in mind that only objects that are instances of this class or
its subclasses are thrown by the Java Virtual Machine (JVM) or can be thrown by
the Java throw statement. Similarly, only this class or its subclasses can be
the argument for the catch statement.
The two main subclasses of Throwable class are as follows.
·
Error
·
Exception
Errors are thrown by the virtual machine when there is a
dynamic linking failure or any other hardware failure. Errors are not normally
thrown by a simple program. Normally programs catch and throw exception derived
for Exception class.
Exception class is again subdivided into two subclasses:
·
IOException
·
RunTimeException
IOExceptions are mainly caused by failed or interrupted input
or output operations. These are mainly of two types:
·
EOFException – This exception is caused when the end of the file
or end of the stream is encountered unexpectedly during input.
·
FileNotFoundException - This exception is caused when a file in a
specified path is not found.
RunTimeExceptions are exceptions that are caused due to some
programming problem that was detected by the runtime device or an inappropriate
use of the application programming interface. These can occur anywhere in a
program. They include:
·
ArithmeticException – This exception occurs when there is some error
in the arithmetic logic- if we have some exception in the arithmetic condition.
An example of this type of exception is trying to divide a number by zero.
·
NullPointerException – This exception occurs when an application
tries to use null where an object should have been used.
IndexOutOfBoundException – This exception occurs when an
index of some sort is out of range. It can be of type:
·
ArrayIndexOutOfBoundException
·
StringIndexOutOfBoundException
The various keywords for handling exceptions are below.
·
try
·
catch
·
finally
·
throw
·
throws