You are on page 1of 32

Exception handling :

three types of errors:


1.compile time errors: there is no harm to the system or program
2.logical errors:
3.runtime errors : because of these errors abnormal termination
of the program
exception means runtime error :
handling: is a mechanism of diverting the processing flow to
another part of the program when exception is occured.
{
-------1
--------2
--------3
}
{
-------
--------
divide by zero: x=a/b;
a=10 b=0
array index outof bounds: a[10]
class name not found.
java developer: he identified all exceptions and he provided
handling mechanism code to users in form of classes
predefined exception classes are available in lang package.
----------
-----------
---c=a/b------3 exception object thrown by jvm

there are two types exceptions:


1.predefined exceptions
2.user defined exception: defined by the user:
bank application: min balance 1000rs

hierarchy of Exception super class:


in lang package
Object
Throwable
Error Exception
(asychronous error) (synchronous errors )
h/w type s/w type
ex:1.stack overflow RuntimeException IOException
checked type unchecked exception
exceptions checked by the compiler at compile time is called as
checked exceptions
exceptions checked by the jvm or interpreter at run time is called
as unchecked exception
unchecked type of exceptions
exception class/exception type meaning
1.ArithmeticException arithmetic error,such
that divide by zero
2.ArrayIndexOutOfBoundsException when we access or
enter data out of array index
3.IllegalArgumentException : illegal argument is used to call a
method
4.IllegalThreadStateException: requested operation is not
compatible with current thread state
5.IndexOutOfBoundsException:index out of bounds
6.NegativeArraySizeException: array created with negative size
7.NullPointerException: invalid use of null reference
8.NumberFormatException: invalid conversion of string to
numeric format
9.StringIndexOutOfBoundsException: access data of of out of
string index
10.FileNotFoundException: file is not accessible or not open
checked type exceptions
1.ClassNotFoundException : mismatch of class name and file
name
2.IllegalAccessException : access to a class is denied
3.InterruptedException: one thread interrupted by another thread
4.NoSuchMethodException: mismatch of method name is there
so accessing is not possible
5.IOException: throw the exception when an input/output
operation is failed,error is occured or interrupted

in java ,exception handling is takes place by using five


keywords
1.try
2.catch
3.finally
4.throw
5.throws
1.try : we can place the set of Statements or single stmt also.
syntax:
try
{
-------------------1
---------------------2
-----------------------3
}

ex 1:
class xyz
{
public static void main(String args[])
{
int a=10;
int b=0;
try
{
int c=a/b;
System.out.println(c);
}
System.out.println("this is try keyword demo");
System.out.println("exit");
}
}
error: arithmetic error message

----------------------------------------------------
2. catch: is used for handling the exception object
syntax:
catch(exceptiontype object)
{
-------------------
-----------------------
}
catch block is executed when the exception is raised in the try
block.
if the exception is not raised in try block than catch block cannot
be executed.
ex2:
class xyz
{
public static void main(String args[])
{
int a=10;
int b=0;
try
{
int c=a/b; // internally jvm can throw that exception //
//object from try block//
System.out.println(c);
}
catch(ArithmeticException e)
{
System.out.println("this is catch block");
}
System.out.println("this is try keyword demo");
System.out.println("exit");
}
}
op: this is catch block
this is try keyword demo
exit
---------------------------------------------
ex 4:
import java.util.*;
class xyz
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("enter a and b values);
int a=s.nextInt();
int b=s.netInt();
try
{
int c=a/b; // exception raised//
System.out.println(c);
}
catch(Exception e)
{
System.out.println("this is catch block");
}
System.out.println("this is try keyword demo");
System.out.println("exit");
}
}
op
1.enter a and b values:
10 5
2
this is try keyword demo
exit
2.enter a and b values:
10 0
this is catch block
this is try keyword demo
exit
----------------------------------------------------
finally: finally block will execute by the jvm irrespective of the
exception is raised or not in the try block . means the finally
block will be executed compulsary.
syntax:
finally
{
------------------------
------------------------
-----------------------
}
import java.util.*;
class xyz
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("enter a and b values);
int a=s.nextInt();
int b=s.nextInt();
try
{
int c=a/b; // exception raised//
System.out.println(c);
}
catch(ArithmeticException e)
{
System.out.println("this is catch block");
}
finally
{
System.out.println("this is try keyword demo");
System.out.println("exit");
}
}
}
op
enter a and b values
10 2
-------------------------------------------------------
import java.util.*;
class xyz
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("enter a and b values);
int a=s.nextInt();
int b=s.netInt();
try
{
int c=a/b; // exception raised//
System.out.println(c);
}
finally
{
System.out.println("this is finally block");
}
System.out.println("this is try keyword demo");
System.out.println("exit");
}
}
op:
enter a and b values
10 0
this is finally block
arithmetic exception :/by zeo
----------------------------
1we can able to use try without catch but here exception
handling is not workout in the program
2. when we want to use exception handling in the program we
have to use try-catch or try-finally
3.we cannot use catch without try and finally without try
-------------------------------------
difference between final, finally, finalize()
---------------------------------------------------------
throw keyword: throw the exception object from try block.it
will be catched in catch block.
throw keyword used two ways
implicit throw: in this we cannot write throw keyword
explicitly.jvm throw exception object from try block.
this is possible only in predefined exceptions
explicit throw: user give the instruction to jvm that throw the
exception object from the try block.
we write throw keyword explicity . throw keyword specify in try
is mandatory in user defined exceptions.
syntax:
1.exceptiontypeclass obj =new exceptiontypeclass();
throw obj;
2. throw new exceptiontypeclass();
ex:
import java.util.*;
class xyz
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("enter a and b values);
int a=s.nextInt(); 10
int b=s.nextInt(); 2
try
{
if(b==0)
throw new ArithmeticException();
else
{
int c=a/b;
System.out.println(c);
}
}
catch(ArithmeticException e)
{
System.out.println("this is catch block");
}
System.out.println("rest of programming part");
}
}
op:
----------------------------------------------------------
throws keyword:
is used to the method block.
syntax:
returntype methodname(plist) throws exceptiontype
{
-----------------------
-----------------------
--------------------
}
example:
class xyz
{
void m1() throws ArithmeticException
{
int a=10; //u can enter data from input device//
int b=0;
int c=a/b;
System.out.println(c);
}
}
class demo
{
public static void main(String args[])
{
xyz obj=new xyz();
try
{
obj.m1();
}
catch(ArithmeticException e)
{
System.out.println("catch block");
System.out.println("caught arithmetic exception");
}
System.out.println("remaining part");
}
}
op:
------------------------------------------------------------
multiple catch: when a try block generates two or more
exceptions,we can associate exception handlers with a try block
by providing more than one catch blocks
try
{
--------------
------------
----------
}
catch( exceptiontype1 e)
{
---------------
}
catch( exceptiontype2 e)
{
----------------------
}
catch(exceptiontype3 e)
{
---------------_
--------------------
}
example:
class xyz
{
public static void main(String args[])
{
try
{
int a=10;
int b=0;
int c=a/b;
System.out.println("op is" + c);
int i[]= null;
int j=i[5];
int x[]={ 2,3,1,4};
x[20]=10;
}
catch( NullPointerException e)
{
System.out.println("catch block of nullpointer exception");
}
catch(ArithmeticException e)
{
System.out.println("catch block of arithmetic exception");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("catch block of array index out of bounds
exception");
}
System.out.println(" rest of code");
}
}
--------------------------------------------------------------
Nested try: we can able to place one try block in another try
block.
syntax:
try //outer try//
{ ----------
-----------------
try //inner try//
{
----------------
-------------------
}
catch( exeptiontype e) // inner try catch//
{----------
}
} // close of outer try//
catch (exceptiontype e) //outer try catch//
{
--------------------
---------------------
}
example:
class demo
{
public static void main(String args[])
{
try
{
int a[]={20,30,25,40};
System.out.println(a[2]);
try
{
int x=20;
int div=x/0;
System.out.println(div);
}
catch( ArithmeticException e)
{
System.out.println("inne try block exception caught");
}
} //outer try close//
catch(ArrayIndexOutOFBoundsException e)
{
System.out.println("outr try block exception caught");
}
System.out.println(" remaining part");
}
}
op:

-------------------------------------------------------------
userdefined exception: custom exception:
when an exception is created by the user is called user defined
exception.
when user wants to create his/her own exception:
steps:
1. user define/create exception class by extending Exception
super class.(extends keyword)
2. create userdefined exception class constructor(
default/parameterised) and call super class constructor by using
super keyword
3. we use throw keyword explicitly in program to throw the
exception object
syntax:
class exceptionclass extends Exception
{
exceptionclass(String s)
{
super(s);
}
}

example:
class myexception extends Exception
{
myexception( String s)
{
super(s);
}
}
class demo
{
public static void main(String args[])
{
try
{
int accno[]={ 1,2,3,4,5};
String name[]={"a","b","c","d","e"};
int bal[]={1000,1050,2000,900,1000};
for(int i=0;i<5;i++)
{
System.out.println(accno[i] +" "+name[i]);
if (bal[i]<1000)
{
throw new myexception(" accno balance is less than 1000
check");
}
else
{
System.out.println(bal[i]);
}
}
}
catch(myexception e)
{
System.out.println(" user defined exception caught");
System.out.println(e); //myexception:accno balance is less than
1000
}
System.out.println("remaining part");
}
}-
---------------------------------------------
create one userdefined exception for check addition of two no
should be greater than 10, othrwise throw the user defined
exception.
-------------------------------------------
rethrow an exception: first throw the exception from try block,it
will be catched in catch block
2. again we have to throw the exception object from catch block
syntax:
try
{
------------
-------------
throw exception object;
}
catch(exceptiontype e)
{
throw exception object;
}

example:
class xyz
{
void m1()
{
try
{
int a=10;
int b=0;
int c=a/b;
System.out.println(c);
}
catch(ArithmeticException e)
{
System.out.println("exception caught");
throw e;
}
}
}
class demo
{
public static void main(String args[])
{
xyz obj=new xyz();
try
{
obj.m1();
}
catch(ArithmeticException e)
{
System.out.println("catch block");
System.out.println("caught rethrown exception");
}
System.out.println("remaining part");
}
}

Termination and resumptive model:


1. used try and catch:
2. try{
------
-----------
exception raised---------
}
catch()
{
-------------------
System.exit();
}
--------------------------
---------------- not possible

You might also like