Professional Documents
Culture Documents
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");
}
}