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

Editorial / Best Answer

Answered by: sri

  • 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

  • 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

  Was this answer useful?  Yes

Neeraj Dua

  • 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.

  Was this answer useful?  Yes

Nizam

  • 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?

  Was this answer useful?  Yes

Dooze

  • Jan 29th, 2007
 

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

  Was this answer useful?  Yes

suniljec

  • 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)

  Was this answer useful?  Yes

shmlbbt

  • Jun 28th, 2008
 

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
}

nirala_it

  • Aug 26th, 2008
 

"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.

  Was this answer useful?  Yes

dineshraj

  • Jan 12th, 2009
 

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

  Was this answer useful?  Yes

muthud

  • 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().

  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.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions