In Execption handling What is exactly difference between throws and throw?

Questions by srikanth.sunjava

Showing Answers 1 - 19 of 19 Answers

realneeraj

  • Dec 28th, 2005
 

'Throws' is used while defining a method to indicate the exceptions that can be thrown by that method. 'Throw' is used to throw an exception.

  Was this answer useful?  Yes

sree_aj

  • Dec 28th, 2005
 

 

throw is used to throw an exception manually, where as throws is used in the case of checked exceptions, to reintimate the compiler that we have handled the exception. so throws is to be used at the time of defining a method and also at the time of calling that function, which rises an checked exception.

Prog. to explain diff. b/w  throw and throw:

class MyException extends Exception //to create our own exception
{
public String toString()   //overriding the method toString() to print the desired msg.
{
return "Can not divide a no. with one: "+"MyException";
}
public static void main(String args[]) throws MyException //use of throws
{
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
if(b==1)
throw new MyException();  // rises an MyException, if we try to divide a no. with 1
else
System.out.println((float)a/b);
}
}

if we want to rise our own exception, we have to use either throws or to handle the exception by try-catch. if not, it gives the compile time error.

and throw is to rise the exception manually, In the above prog. I rised an exception when you try to divide a no with 1.(own Exception)

     ----sri

  Was this answer useful?  Yes

Throw as well as Throws they sound similar but there is diff among them.Both of them are keywoeds used in JAVA and are related with exceptions.

THROW - this keyword is used to throw an exception explicitly.

                -for eg consider we catch an Divide by zero exception in any method.If  v wish     to   throw this exception to the method which called it then v will throw the exception obj to the method which called it.

THROWS-See this keyword provide us with an alternative to catch exception.

              - v can catch exceptions through try{} catch{} too,but if  v expect an exception

               which we may not know then write this keyword in front of the method.If exceptn

              occurs then it will b throwed to the calling method

 

             

  Was this answer useful?  Yes

kishorek.a

  • Feb 2nd, 2006
 

THROW- it is used to throw an exception mannualy or explicitly.

THROWS-this is used if you dont want a particular function should not handle any exception that it can throw, then you can specify a thows clause in the methods declaration.the calling function should gaurd itself by having respective try catch block.

  Was this answer useful?  Yes

manohar

  • Mar 9th, 2006
 

Throws------ this keyword shows some exception so that it can make the client alert by showing the particular exception the program is going 2 throw.Throw---- this is used to throw some exceptions explicitly by the user.

  Was this answer useful?  Yes

santukt

  • Apr 15th, 2006
 

Throws throws the Exception object to the java Runtime system from within a method scope and the default Exception handler handles that.But throw throws Exception object mannually out of the try block and programmer has to provide code for handling that(ie 1 catch or finally)

  Was this answer useful?  Yes

kranthi

  • Sep 5th, 2007
 

As per my knowledge, When we use Throws keyword need not to catch the exceptions by the programmer. The JVM will take care about these exceptions.
When we use Throw keyword definitely the programmer should catch the exception and give the appropriate message to the user.

  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