Exception Handling
Exception Handling
Exception
Exception is an abnormal condition that arises at run time
Event that disrupts the normal flow of the program.
It is an object which is thrown at runtime.
Exception Handling
Exception Handling is a mechanism to handle runtime errors.
Normal flow of the application can be maintained.
It is an object which is thrown at runtime.
Exception handling done with the exception object.
Types of Errors
There are three categories of errors:
Syntax errors - arise because the rules of the language have not been
followed. They are detected by the compiler.
Logic errors - occur when a program doesn't perform the way it was
intended to.
Types of Exception:
There are mainly two types of exceptions:
Checked
Unchecked – Eg. error
The sun microsystem says there are three types of exceptions:
Checked Exception - are checked at compile-time.
Unchecked Exception - are not checked at compile-time rather
they are checked at runtime.
Error
Checked Exception - Classes that extend Throwable class except
RuntimeException and Error are known as checked exceptions.
Checked Exceptions means that compiler forces the programmer to
check and deal with the exceptions. e.g.IOException, SQLException
etc.
Unchecked Exception - Classes that extends RuntimeException, Error
and their subclasses are known as unchecked exceptions e.g.
ArithmeticException,NullPointerException, ArrayIndexOutOf Bounds
Exception etc.
Error is irrecoverable should not try to catch. e.g. OutOfMemoryError,
VirtualMachineError, AssertionError etc.
Exception Classes
ClassNotFoundException
IOException
ArithmeticException
Exception AWTException
NullPointerException
RuntimeException
IndexOutOfBoundsException
Object Throwable Several more classes
IllegalArgumentException
VirtualMachineError
Error
AWTError
IOException
ArithmeticException
Exception AWTException
NullPointerException
RuntimeException
IndexOutOfBoundsException
Object Throwable Several more classes
IllegalArgumentException
6
Exceptions
Exception describes errors ClassNotFoundException
caused by your program
and external IOException
circumstances. These ArithmeticException
errors can be caught and Exception AWTException
handled by your program. NullPointerException
RuntimeException
IndexOutOfBoundsException
Object Throwable Several more classes
IllegalArgumentException
VirtualMachineError
Error
AWTError
7
Runtime Exceptions
ClassNotFoundException
IOException
ArithmeticException
Exception AWTException
NullPointerException
RuntimeException
IndexOutOfBoundsException
Object Throwable Several more classes
IllegalArgumentException
8
Exception Handling Terms
try – used to enclose a segment of code that may produce a
exception
catch – placed directly after the try block to handle one or more
exception types
If more than one exception can occur, then we use multiple catch
blocks
If an inner try statement does not have a catch, then the next try
statement’s catch handlers are inspected for a match
If a method call within a try block has try block within it, then then
it is still nested try
Nested Try Block
try
{
statement 1;
statement 2;
try
{
statement 1;
statement 2;
}
catch(Exception e)
{
}
}
catch(Exception e)
{
}
class Excep6
{
public static void main(String args[])
{
try
{
try
{ catch(ArrayIndexOutOfBoundsException e)
S.o.p("going to divide"); {
int b =39/0; System.out.println(e);
} }
catch(ArithmeticException e) System.out.println("other statement");
{ }
System.out.println(e); catch(Exception e)
} {
try System.out.println("handeled");
{ }
int a[]=new int[5]; System.out.println("normal flow..");
a[5]=4; }
} }
Finally block
is a block that is always executed.
To perform some important tasks such as closing connection, stream etc.
Used to put "cleanup" code such as closing a file, closing connection etc.
Finally creates a block of code that will be executed after a try/catch block
has completed
Finally block will be executed whether or not an exception is thrown.
Each try clause requires at least one catch or finally clause.
Note: Before terminating the program, JVM executes finally block(if any).
Note: finally must be followed by try or catch block.
class Simple
{
public static void main(String args[])
{
try
{
int data=25/0;
System.out.println(data);
}
catch(ArithmeticException e)
{
System.out.println(e);
}
finally
{
System.out.println("finally block is always executed");
}
System.out.println("rest of the code...");
}}
throw keyword
keyword is used to explictily throw an exception / custom exception.
throw new ExceptionName("Error Message");
Throw either checked or uncheked exception.
throw new ThrowableInstance
ThrowableInstance must be an object of type Throwable /
subclass Throwable
NumberRangeException()
{
msg = new String("Enter a number between 20 and 100");
}
}
public class My_Exception
{
public static void main (String args [ ])
{
try
{
int x = 10;