Exception Handling
Managing
Errors and Exceptions
Exception
occurs only at the execution state of the program and can be generated in a
java program if:
(a)
the
array index crosses the range
(b)
a
number is divided by Zero
(c)
the
file one is trying to open , does not exist
(d)
the
class file which a programmer wants to load is missing, etc.
some of the exceptions are
detected at complied time and some others during the run time. In java
exceptions that are detected at compile time are known as checked exceptions
and exception that are detected during execution are known as unchecked
exceptions.
In java
Exception handling is done by five key words
try
catch
throw
throws
finally
try
{
//block of code to monitor
for errors
}
catch(Exception_Type_1
excp1)
{
//codes to handle the
exception
}
catch(Exception_type_2
excp2)
{
//codes to handle the
exception
}
catch(Exception_type_3
excp3)
{
//codes to handle the
exception
}
……
……
finally
{
//codes that have to be
executed must
}
Types of Errors & Exceptions
- ArrayIndexOutOfBoundsException
- ArrayStoreException
- ClassCastException
- IllegalArgumentException
- IllegalMonitorStateException
- IllegalStateException
- IllegalThreadStateException
- IndexOutOfBoundsException
- NegativeArraySizeException
- NullPointerException
§ NumberFormatException
§ SecurityException
§ StringIndexOutOfBounds
§ UnsupportedOperationException
§ ClassNotFoundException
§ CloneNotSupportedException
§ IllegalAccessException
§ InstantiationException
§ NoSuchFieldException
§ NoSuchMethodException
Example
for Arithmetic Exception:
class
arith
{
public
static void main(String args[])
{
int
a=5,b=5,c=10;
try
{
int
z=c/(a-b);
System.out.println("Z
value="+z);
throw
new ArithmeticException();
}
catch(ArithmeticException
e)
{
System.out.println(e);
}
}
}
Example
Array Out Of Bounds Exception
class
array
{
public
static void main(String args[])
{
int a[]=new int[5];
ArrayIndexOutOfBoundsException
ae=new ArrayIndexOutOfBoundsException("Not In Range\t");
for(int i=0;i<5;i++)
{
a[i]=i+1;
}
try
{
for(int
i=0;i<10;i++)
{
System.out.println(a[i]);
}
throw ae;
}
catch(ArrayIndexOutOfBoundsException
e)
{
System.out.println(e);
}
}
}
Example for
Illegal argument Exception
class
MyClock
{
int
hours ,minutes, seconds;
MyClock(int hours, int minutes, int seconds)
{
if
(hours < 1 || hours > 12)
{
throw
new IllegalArgumentException("Hours must be between 1 and 12");
}
if
(minutes < 0 || minutes > 59)
{
throw
new IllegalArgumentException("Minutes must be between 0 and 59");
}
if
(seconds < 0 || seconds > 59)
{
throw
new IllegalArgumentException("Seconds must be between 0 and 59");
}
this.hours
= hours;
this.minutes
= minutes;
this.seconds
= seconds;
System.out.println(hours+"hrs:"+minutes+"mins:"+seconds+"sec;");
}
public
MyClock(int hours, int minutes)
{
this(hours,
minutes, 0);
}
public
MyClock(int hours)
{
this(hours,
0, 0);
}
}
public
class ThrowTest
{
public
static void main( String args [])
{
try
{
MyClock
clock = new MyClock(1, 10,20);
MyClock
c=new MyClock(1);
MyClock
a=new MyClock(20,70,40);
}
catch(
IllegalArgumentException e)
{
System.out.println("IllegalArgumentException
is caught...."+e.getMessage());
}
}
}
Multiple
Catch Statements
Sometimes
there may be a chance to have multiple exceptions in a program. You can use
multiple catch clauses to catch the different kinds of exceptions that code can
throw. If more than one exception is raised by a block of code, then to handle
these exceptions more than one catch clauses are used. When an exception is
thrown, different catch blocks associated with try block inspect in order and
the first one whose type (the exception type passed as argument in catch
clause) matches with the exception type is executed.
Using
Finally Statement
A finally block is always
executed, regardless of the cause of exit from the try block, or whether any
catch block was executed. Generally finally block is used for freeing
resources, cleaning up, closing connections etc. Any statements in the body of
the finally clause are guaranteed to be executed.
Throwing our
own Exceptions:
There
may be times when we would like to throw our own exceptions. We can do this by
using the throw keyword.
class MyException
extends Exception
{
String
s1;
MyException(String
s1)
{
this.s1=s1;
}
public
String toString()
{
return(s1);
}
}
class
Myclass
{
public
static void main(String args[])
{
int
marks[]={59,33,99,100,97};
int
stno[]={1,2,3,4,5};
try
{
for(int
i=0;i<6;i++)
{
if
(marks[i]>100)
throw
new MyException("invaid data");
// throwing user-defined exception
if
(i<4)
System.out.println("rollno="+stno[i]+"\tmarks="+marks[i]);
else
throw
new ArrayIndexOutOfBoundsException("Array out of bnds");
}
}
catch(MyException
e)
{
System.out.println("Exception="+e);
}
catch(ArrayIndexOutOfBoundsException
a)
{
System.out.println("Index
out of range"+a);
}
finally
{
System.out.println("finished");
}
}
}