what is difference between throw and throws in exception?

Answers were Sorted based on User's Feedback



what is difference between throw and throws in exception?..

Answer / chirag

throw will throws the exception,
and throws declares that the function will throw an exception
you should catch all every functions throwing and exception
u declare it or catch it

Is This Answer Correct ?    29 Yes 25 No

what is difference between throw and throws in exception?..

Answer / manu

throw: It is used to explicitly or manually throw an
exception.it can throw user defined exceptions.

throws:It tells that exception to b handled by calling
function. It tells caller that what exception that method
could throw.

Is This Answer Correct ?    4 Yes 0 No

what is difference between throw and throws in exception?..

Answer / sinduja

THROW IS USED TO THROW AN EXCEPTION EXPLICITLY WHEREAS,THROWS INCLUDED IN THE METHODS DECLARATION PART WITH A LIST OF EXCEPTION THAT IT CAN POSSIBLE TO THROW.

THROW IS USED TO PASS CUSTOMER MESSAGE.AND THROW HANDLES
USER DEFINE EXCEPTION.
WHEREAS THROWS IS HANDLED BY JVM.

Is This Answer Correct ?    4 Yes 0 No

what is difference between throw and throws in exception?..

Answer / raju

throw : throw is used to throw user defined exceptions.
For ex: MyException written by user.it may be either
checked or unchecked exception.

throws: throws is used to throw exception those are handled by
JVM directly.
throws is used in method declaration to
intimate user that, it throw the exception that must
be handled by calling method.

Note :use throws when your method does not handle exception.

Is This Answer Correct ?    4 Yes 1 No

what is difference between throw and throws in exception?..

Answer / prasanna

throw - It is used to throw an Exception.
throws - This is used to specifies that the method can
throw Exception

Is This Answer Correct ?    3 Yes 0 No

what is difference between throw and throws in exception?..

Answer / savita patil

throw is used for to through exception system explicitly,and
throws is u sed for to throws exception means throws
ioexception and servletexception and etc.........

Is This Answer Correct ?    3 Yes 1 No

what is difference between throw and throws in exception?..

Answer / gourav shivhare

The throw keyword denotes a statement that causes an exception to be initiated. It takes the Exception object to be thrown as an argument. The exception will be caught by an enclosing try-catch block or propagated further up the calling hierarchy. The throws keyword is a modifier of a method that denotes that an exception may be thrown by the method. An exception can be rethrown.

Is This Answer Correct ?    1 Yes 0 No

what is difference between throw and throws in exception?..

Answer / amol nakhwa

/*
Save this file as "TestCircle.java"
*/



class NegativeRadiusNotAllowedException extends Exception
{
public String getMessage()
{
return "Radius should be positive number only!";
}
}

class Circle
{
public void calculateArea(int rad) throws
NegativeRadiusNotAllowedException
{
if (rad < 0)
{
throw new NegativeRadiusNotAllowedException();
}
double ar = 3.1415*rad*rad;
System.out.println("Circle Radius = "+ rad + " and Area =
"+ar);
}
}



public class TestCircle
{
public static void main (String args[])
{
Circle c = new Circle();

try
{
//int no1 = Integer.parseInt(args[0]);
//int no2 = Integer.parseInt(args[1]);
c.calculateArea(9);
c.calculateArea(-9);
}
catch (NegativeRadiusNotAllowedException e)
{
System.out.println(e.getMessage());
}
catch (NumberFormatException e1)
{
System.out.println(e1.getMessage() +" is not a valid
integer");
}
catch (Exception e2)
{
System.out.println(e2.getMessage());
}
}
}

Is This Answer Correct ?    1 Yes 0 No

what is difference between throw and throws in exception?..

Answer / sathiya moorthy v

throw --> manually throw exception.
Ex: throw ThrowableInstance;
throws -->Any exception that is thrown out of a method must
be specified as such by a throws clause
Ex: <access specifier> <return type> <methodName> throws
Exception{
}

Is This Answer Correct ?    1 Yes 0 No

what is difference between throw and throws in exception?..

Answer / basha

throw & throws is a keyword in Exceptions.
throw is to throw the exception defined by User.
Ex:if user enter wrong pin number,it shows error
(Exception). This s the exception used to throw it.

And throws is that which will throw the error as the Cash
Dispenser not having the cash to dispense. This type of
error(exception) is defined by throws keyword.

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More Core Java Interview Questions

Explain Event handling in AWT?

1 Answers   Infosys, TCS,


How dead lock situation occurs in java and how you can identify it?

0 Answers  


we know that every java prog must follows OOPS Principles. Anybody can answer, HOW THE FOLLOWING PROGRAM FOLLOWS OOPS CONCEPTS i.e, Inheritance,Polymarphism,Encapsulation? class a{ public static void main(String args[]){ System.out.println("Hello World"); } }

3 Answers  


what is Dictionary? and what purpose it is used for?

3 Answers  


Explain super keyword in java.

0 Answers  






What is :: operator in java 8?

0 Answers  


What do you mean by an interface in java?

0 Answers  


What is lambda expression in java?

0 Answers  


What is static import in java?

0 Answers  


How does JAVA ClassLoader work?

1 Answers   IBM,


Why are there no global variables in java?

0 Answers  


why String class is immutable.

5 Answers   iGate,


Categories