What is difference between try and throws, as these are two different methods to handle the exceptions.

Showing Answers 1 - 2 of 2 Answers

Supraja

  • Jul 10th, 2006
 

try / catch is used to guard against possible errors or problems. At the minimum the error should at least be reported to the user or logged in some way. More advanced systems will attempt to recover from the error and continue with normal operation.

"Throws" simply passes the exception up the chain to be dealt with. It can be throw up many times.

  Was this answer useful?  Yes

Guest

  • Oct 1st, 2006
 

Using of the try / catch we can handle any exception whether it is checked or unchecked exception. Using of throws we can handle only checked exceptions. throws means throwing the exception to calling functon . at the place we must handle exception. At that place also we can use throws .if use throws  all places it finall goes to jvm .jvm sutdown this exception.

EX:

class Ex1

{

public void divide() throws Exception

{

int c= 2/0;

}
}
class Sample3

{

public static void main(String args[]) throws Exception

{

Ex1 e1=new Ex1();

e1.divide() ;
}

}
             


 

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions