GeekInterview.com
Series: Subject: Topic:
Question: 255 of 1115

What is the difference between throw and throws clause, explain in programatically

Asked by: Interview Candidate | Asked on: Dec 23rd, 2005

Editorial / Best Answer

Answered by: sri

Answered On : Dec 24th, 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

Showing Answers 1 - 11 of 11 Answers
sri

Answered On : Dec 24th, 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

  
Login to rate this answer.
Neeraj Dua

Answered On : Feb 2nd, 2006

keyword "throw" is used to through Use Defined Exception while keyword "throws" is used when we want that exception is to be handled by calling function.

  
Login to rate this answer.
Nizam

Answered On : Feb 14th, 2006

What is the reason for throwing exception? for the above example, the divide by 1 can be handeled by simple print statement but y we want to throw an exception.

I know throw is used to through an exception but y?

any real time example where it is really necessary?

  
Login to rate this answer.
Dooze

Answered On : Jan 29th, 2007

View all answers by Dooze

well, you may not want a particular method to do exception handling either because there are way too many things done by the method or sometimes it is by practice to make the top caller in a call stack to handle the exception. in cases like this your intermediate methods in the call stack will want o just throw exceptions so that other methods in the call stack can take the responsibility of catching/handling them.call stack means the order in which methods are call.eg if method m1 calls m2 cals m3 calls m4...this is a call stack.m4 can throw an exception to m3, then m3 can also throw the exception to m2 and finally m2 can throw that exception to m1 and m1 will have to handle/catch this exception.so as you can see there are two ways here to deal with exceptions1. throw it to a caller using throw and throws appropriately2. don't throw it, handle it yourself(refering to a method) using the try-catch-finally block

  
Login to rate this answer.
suniljec

Answered On : Aug 12th, 2007

throw is used to throw an exception manually, where as throws is used in the case of checked exceptions, to re intimate 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)

  
Login to rate this answer.
shmlbbt

Answered On : Jun 28th, 2008

View all answers by shmlbbt

The throws clause is part of a method declaration. It tells callers of that method what exceptions that method could throws.

A throw statement actually throws the exception.

void foo() throws BarException { // throws clause says this method [i]might[/i] throw BarException
do some stuff
if (some bar-like condition is met) {
throw new BarException("Oh No! It's Bar!"); // throw statement actually throws the exception
}
 
we only get here if we didn't throw the exception, so we go on executing this method
}

Yes  1 User has rated as useful.
  
Login to rate this answer.
nirala_it

Answered On : Aug 26th, 2008

View all answers by nirala_it

"throws clause" is the way to specify exceptions, which means to notify the calling method that the callee might throw some type of exceptions somewhere.
 
"throw clause" is to define which part of the code is actually throwing exceptions.

  
Login to rate this answer.
dineshraj

Answered On : Jan 12th, 2009

View all answers by dineshraj

Throws clause can be used with method protyping but throw can't be used like


public void method()throws Exception;

throws clause is used to handle the checked exception if we are not willing to handle that it will be compiler responsibility to handled them because these are known by complier

  
Login to rate this answer.
gyanguru

Answered On : Aug 4th, 2009

View all answers by gyanguru

Throw is used for a single exception
Throws is use for multiple exception.

Yes  1 User has rated as useful.
  
Login to rate this answer.
micklesharma

Answered On : Jul 6th, 2010

View all answers by micklesharma

Throws is a userdefined exception handling whereas throw is system defined....

  
Login to rate this answer.
muthud

Answered On : Nov 22nd, 2011

Exception handling is done by two ways . One is handled by JVM and another is handled by User.

JVM does Exception handling by using principle of Exception can be Caught or Declared . Caught is nothing but using 'try & catch block' . Declaring is nothing but 'throws'. That is , In 'throws', it declares that this method may throw an exception but it should be Caught somewhere in the code.

But , User wants to throw his own exception then he will use ' throw throwableInstance'.
Example : throw new MyException().

  
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.