Difference between Throw & Throws

1)What is the difference between Throw & Throws?
2)Explain the use of transient variable with example?

Questions by eldhose222

Showing Answers 1 - 4 of 4 Answers

when we are using thows when exception coms thatperticular exceptions handle by JVM.

using throw you can throw the perticular exception.

like:

 try {
       //..
      
     }catch(xxException e) {
       //..
       throw new xxxException("xxx");
     }catch(Excepiton ee) {
       //..
     }finally {
       try {
        //..
       }catch(xxException eee) {
        //..
       }
     }

You use the Transient keyword to indicate to the Java virtual machine that the indicated variable is not part of the persistent state of the object.

when your serialization that object it can't persistent that value

  Was this answer useful?  Yes

Naveen Muniraja

  • Sep 21st, 2006
 

hi there,throws::::(DECLARATION) the way we decalre that exception might occur...... it is a caution to the JVM so that it prepares to handle the Exceptionthrow::::: if at any stage of coding we r sure that an exception might occur and unfortunately if we donot know how to handle that exception then we THROW the Exception....... so here by we make sure the control is in our handtransient::::During the process of serialization if u donot want a particular variable to be serialized then we make that variable TRANSIENT

  Was this answer useful?  Yes

arunagar

  • Dec 8th, 2009
 

For Question 1 - Throw we put in Catch block, when we want to throw the exception occured.
Throws is written in the signature of the method, when we are anticipating some execption and we want that to be thrown to the calling class.

If in method you have mentioned 'Throws' then no need to put try/catch block. The error will automatically thrown to the calling class.

They are mainly used when you want to have the user defined exception

For Question 2) Transient is used only with the member variables and it make sense also, as by making them transient we are telling that we dont want its state(value) to be saved. As methods and class are not state so we dont have anything to save them there this keyword is not used with them.

  Was this answer useful?  Yes

Ans.1)With the use of 'throw' it mean that we are simply throwing the exception object(this is explicitly).but using 'throws' with method signature it mean that exception object is thrown implicitly by the method.

  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