Professional Documents
Culture Documents
is called exception
tyrepunctured expection
All the exceptions are pre-defined classes. All Exception types are sub
classes of the built-in class Throwable . Immediately below Throwable class, two
subclasses Exception and Error. Important subclass of Exception called
RuntimeException, from which all the exceptions have been derived. Error class,
which defines exceptions that are not to be caught by our programs.
Java exception handling is managed via five keywords. try, catch, throw, throws
and finally.
2. Unchecked Exceptions :- These are the runtime errors that occur because of
programming errors.
Ex: ArithmeticException, ArrayIndexOutOfBoundsException,
IllegalArgumentException, NullPointerException, NumberFormatException etc
try
{
-executable statements which may throw an exception
}
catch(Exception e)
{
- some message -
}
finally
{
--statements will be executed compulsorily --
}
You can have any number of try blocks in a program. we can use nested try
block also, i.e. try block inside a try block.
Inside the try block, once the exception is raised immediately the control will be
thrown out of the try block. It doesn't allow the control to go to the next
statement in try block.
So catch should be followed immediately after the try block, to catch the
exceptions. Finally also can be written after try block.
printing a stack trace :- We can find out the line or method, where the exception
is raised by using printStackTrace() method in catch block.
eg: catch(ArithmeticException e)
{
e.printStackTrace();
}