EXCEPTION HANDLING
Exception handing is the mechanism to handle runtime malfunctions. We need to handle such exceptions to
prevent abrupt termination of program. The term exception means exceptional condition, it is a problem that
may arise during the execution of program.
A java exception is an object that describes the exception that occurs in a program. When an exceptional event
occurs in java, an exception is said to be thrown. The code that is responsible for doing something about the
exception is called an exception handler.
Exception class hierarchy
Throwable
Exception
Runtime Exception classes
3 categories of exceptions
a) Checked Exceptions – the exception that can be predicted by the programmer. Example – variable is
not declared but used.
b) Uncheck Exceptions – These are runtime exceptions, generally not caught at compile time. Example
– ArithmeticException.
c) Error – Error are typically ignored in code because these are occurred due to system resources and
cannot be handled in the code.
Exception handling mechanism
There are 5 keywords used to handle exceptions in java.
1. Try
2. Catch
3. Throws
4. Finally
5. throw
“Exception handling is done by transferring the execution of a program to an appropriate exception handler
when exception occurs.”
Try-catch
try is used to guard a block of code in which exception may occur. This block of code is called
guarded region.
A catch statement involves declaring the type of exception you are trying to catch.
If the exception occurs in the guarded code, the catch block that follows the try is checked, if the type
of exception that occurred is listed in the catch block then the exception is handed over to the catch
block which then handles it.
Example
class test
{
public static void main(String a[])
{
Int a,b,c;
try
{
a=0;
b=10;
c=b/a;
[Link](“Value of C:”+c);
}catch(ArithmeticException e)
{
[Link](“Divide by 0”);
}
}
}
Multiple catches
class test
{
public static void main(String a[])
{
try
{
int a[]={1,2};
a[2]=3/0;
}catch(ArithmeticException e)
{
[Link](“Divide by 0”);
}
catch(ArrayIndexOutOfBoundException e)
{
[Link](“Index of array is not defined”);
}
}
}
Throws
Using this keyword or method we can give prior knowledge about which exception to handle.
Method_name (parameters ) throws Exceptionname
{
}
Example
class test
{
void divide() throws ArithmeticException
{
a=0;
b=10;
c=b/a;
[Link](“value of c:”+c);
}
public static void main(String a[])
{
test t1=new test();
[Link]();
}
}
Finally
A finally keyword is used to create a block of code that follows a try block. A finally block of code always
executes whether or not execution has occurred. Using a finally block, lets you run any cleanup type
statements that you want to execute.
Example
class test
{
public static void main(String a[])
{
Int a[]=new int[2];
[Link](“Initialize”);
Try
{
[Link](a[3]);
}catch(Exception e)
{
[Link](“Array index”);
}
Finally
{
[Link](“Close “);
}
}
}
User defined Exception
We can also create our own exception subclass by extending java Exception class. We can override toString()
method to define our own message.
Example
class myexc extends Exception
{
int x;
myexe(int y) // constructor to pass number
{
x=y;
}
public String toString()
{
return “no is > than 10”;
}
}
class demo
{
static void compare(int x) throws myexc
{
if(x>10)
throw new myexc(x);
else
[Link](“x=”+x);
}
public static void main(String a[])
{
try
{
Compare(11);
}catch (myexc e)
{
[Link](e);
}
}
}