Professional Documents
Culture Documents
Exception Handlingnew111
Exception Handlingnew111
Exception Handling
• Exception handling is one of the powerful
mechanism provided in JAVA.
• It provides mechanism to handle exceptions
so that normal flow of application can be
maintained.
• The java.lang.Throwable class is the root class
of Java Exception hierarchy which is inherited
by two subclasses:
• Exception and Error.
Exception Handling
Exception :
An exception is a problem that arises during
the execution of a program.
Exception interrupts the normal flow of
program.
Exception Handling: Exception Handling is a
mechanism to handle exceptions.
Advantage of Exception Handling
• To maintain the normal flow of execution
• e.g.
Statement 1 ;
Statement 2;
Statement 3; without exception handling, rest of the code i.e.
Statement 4 ; statements 6 to 10 will not be executed.
Statement 5; ----error
Statement 6;
Statement 7; using exception handling, rest of the code i.e.
Statement 8; statements 6 to 10 will be executed.
Statement 9; advantage
Statement 10;
Common Java Exceptions
Sr. Exception Cause of Example
No. exception
01 ArithmeticException while int a=10;
performing int b=0;
arithmetic int c;
operations such c=a/b;
as divide by zero //ArithmeticException
…….
…….
02 ArrayIndexOutOfBoundsException Caused by bad int a[]=new int[5];
array index a[10]=40;
//ArrayIndexOutOfBou
ndsException
Common Java Exceptions
Sr. Exception Cause of exception Example
No.
03 NullPointerException Caused by String s=null;
referencing a null System.out.println(“string
object. length=“+s.length(););
//NullPointerException
Java abc.java
error:java.lang.ArithmeticException : / by Zero
Program using exception handling
class abc //System.out.println(“divide by zero”);
{ }
public static void main(String args[])
{ c2=a/d;
int a,b,c1,c2,d; System.out.println(c2);
a=10;
b=0; }
d=2; }
try
{
c1=a/b;
System.out.println(“C1=“+c1);
}
catch(ArithemeticException e)
{
System.out.println(e);
//Or
//System.out.println(e.getMessage());
//Or
Program to handle ArrayIndexOutOfBoundsExcxeption
class abc
{
public stativ void main(String args[])
{
Int a[]=new int[5];
try
{
a[7]=10;
System.out.println(“a*7+=“+a*7+);
}catch(ArrayIndexOutOfBoundsExcxeption e)
{
System.out.println(e);
}
}
}
Program to handle ArrayIndexOutOfBoundsExcxeption
class abc C:\tyif> java abc.java
{
public static void main(String args[]) java.lang.ArrayIndexOutOfBoundsExcept
{ ion :7
Int a[]=new int[5];
try Output of remaining code
{
a[7]=10;
System.out.println(“a*7+=“+a*7+);
}
catch(ArrayIndexOutOfBoundsExcxeption e)
{
System.out.println(e);
}
//remaining code………………..
}
}
Program to handle NullPointerExcxeption
class abc C:\tyif> java abc.java
{
public static void main(String args[]) java.lang. NullPointerExcxeption
{
String str=null; Output of remaining code
int len;
try
{
len=str.length();
System.out.println(“string length=“+len);
}
catch(NullPointerExcxeption e)
{
System.out.println(e);
}
//remaining code………………..
}
}
Program to handle NumberFormatException
class abc C:\tyif> java abc.java
{
public static void main(String args[]) java.lang. NumberFormatException:For input string
{ :“abc”
String str=“abc”;
int i; Output of remaining code
try
{
i=Integer.parseInt(str);
}
catch(NumberFormatException e)
{
System.out.println(e);
}
//remaining code………………..
}
}
Program to handle StringIndexOutOfBoundsException
class abc2 C:\tyif> java abc.java
{
public static void main(String args[]) java.lang. StringIndexOutOfBoundsException:string
{ index of out of range :5
String str="abc";
char ch; Output of remaining code
try
{
ch=str.charAt(5);
}
catch(StringIndexOutOfBoundsExce
ption e)
{
System.out.println(e);
}
//remaining code………………..
}
}
Multiple catch statements
---Multiple catch statements are used to handle different types of exception.
--When an exception in try block is generated, java treats multiple catch
statements like cases in a switch statement.
try .
{ .
Statements; .
} catch(Exception_Type n object)
catch(Exception_Type1 object) {
{ Statements;
Statements; }
}
catch(Exception_Type2 object)
{
Statements;
}
Multiple catch statements
• Multiple catch statements are used to handle different types of exception.
• Syntax:
try
{
Statements;
}
catch(Exception_Type1 object)
{
Statements;
}
catch(Exception_Type1 object)
{
Statements;
}
Program using multiple catch statement