GeekInterview.com
Series: Subject: Topic:
Question: 89 of 573

difference between throw and throws

What exactly is the difference between between throw and throws? If both are used for the same purpose then why are both needed instead of one?
Asked by: Kartheek11 | Member Since May-2008 | Asked on: Jun 11th, 2008

View all questions by Kartheek11

Editorial / Best Answer

Answered by: vegetto

View all answers by vegetto

Member Since Jun-2008 | Answered On : Jun 15th, 2008

hi there,
             
throw:the throw keyword is used to throw the exception manually,that is when you feel a particular line in your code, when executed is capable of having some exception then you use throw keyword as:
 
throw new MyException(arguments if any);
this leads to instantiating of the MyException class and exception is thrown


throws: this is to be used when you are not using the try catch statement in your code but you know that this particular class is capable of throwing so and so exception(only checked exceptions).in this you do not use try catch block but write using the throw clause at appropriate point in  you code and the exception is thrown to caller of the method and is handeled by it. eg:

void trouble()throws IOException,any other exceptions just separate them with commas.......
{
   /* throw statement at appropriate step,
    no need to use try catch here,exception would be thrown to caller and u should    provide try catch block to handle exception there else this process cotinues further till
   appropriately it is handeled or prog terminates abruptly */
}


this is called as HANDLE OR DECLARE RULE  i.e either u handle exceptions using try catch block or by declaring them
 

 

Showing Answers 1 - 2 of 2 Answers

they are not same.

"throws" declares that your method is capable of throwing an exception.

"throw" actually  does the work ,  of throwing the exception.

example :

public void doSomething() throws ApplicationException
{
    try{
        }
    catch(Exception e){
            // catch all excpetions n provide some meaningful message
            throw ApplicationException("An error occurred while trying connect to DB");
        }
}

Yes  6 Users have rated as useful.
  
Login to rate this answer.
vegetto

Answered On : Jun 15th, 2008

View all answers by vegetto

hi there,
             
throw:the throw keyword is used to throw the exception manually,that is when you feel a particular line in your code, when executed is capable of having some exception then you use throw keyword as:
 
throw new MyException(arguments if any);
this leads to instantiating of the MyException class and exception is thrown


throws: this is to be used when you are not using the try catch statement in your code but you know that this particular class is capable of throwing so and so exception(only checked exceptions).in this you do not use try catch block but write using the throw clause at appropriate point in  you code and the exception is thrown to caller of the method and is handeled by it. eg:

void trouble()throws IOException,any other exceptions just separate them with commas.......
{
   /* throw statement at appropriate step,
    no need to use try catch here,exception would be thrown to caller and u should    provide try catch block to handle exception there else this process cotinues further till
   appropriately it is handeled or prog terminates abruptly */
}


this is called as HANDLE OR DECLARE RULE  i.e either u handle exceptions using try catch block or by declaring them
 

 

Yes  9 Users have rated as useful.
  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.