Javatpoint Logo

91-9990449935

 0120-4256464


what is try & catch?

By: alnraghava@gmail.com On: Tue Jun 14 15:29:55 IST 2016     0 0 0  0
how to use try catch in programs? 0

 
Enslose the code ( code which might throw exception) you want to monitor inside the "try" block.
for example- 1/0 ; this throws the Division by zero exception(ArithmeticException)
so put 1/0 in "try" block

void tryDemo() {
try {
int x = 5/0;
} catch(ArithmeticExcption e) {
system.out.println("Exception is " + e);
}

in "catch" clause we specified what type of exception we want to catch and what we want to do after that exception is caught.
0

By: nileshpadm@gmail.com On: Tue Jun 14 17:06:09 IST 2016 0 0 0 0
Are You Satisfied :0Yes0No
 
0

By: nileshpadm@gmail.com On: Tue Jun 14 17:06:41 IST 2016 0 0 0 0
Are You Satisfied :0Yes0No
 
We can handle the exception like the following,

we can handle all the exception in the exception class, because exception is the super class of all the exception whether it is subclass of runtime class or subclass of exception class.


try{
int a=10;
int b=0;

int c=a/b;
}
catch(Exception e)
{
Sop("Exception occurred");
}
0

By: pant8g@gmail.com On: Wed Jun 15 14:31:09 IST 2016 0 0 0 0
Are You Satisfied :0Yes0No


PLEASE REPLY

Please login first to post reply. Login please!