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 / krishna

Throws is a keyword.This is used to say explicitly this
method has doughtful body,where as Throw is used to handle
the exception in next level or itself..............

Is This Answer Correct ?    0 Yes 0 No

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

Answer / visweswara rao

throw:- System generated exceptions are automatically
thrown by the Java run-time system.To manualy throw the
exception,we use the keyword throw

throws:- Any exception that is thrown out of a method must
be specified as such by a throws clause

Is This Answer Correct ?    0 Yes 0 No

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

Answer / chaitanya

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 */
}

Is This Answer Correct ?    0 Yes 0 No

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

Answer / parveen

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 */
}

Is This Answer Correct ?    0 Yes 0 No

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

Answer / arun

throw is used to throw an exception manually when we feel
that particular line of code in our program is going to
throw an exception
throw new MyException("exception")

throws is used instead of try and catch block,if we are not
using try and catch block and we know that our program is
going to throw an exception than we use throws key word

public void myExecption() throws Exception

Is This Answer Correct ?    0 Yes 0 No

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

Answer / rhine

One declares it, and the other one does it.

Throw is used to actually throw the exception, whereas throws is declarative for the method. They are not interchangeable.
public void myMethod(int param) throws MyException
{
if (param < 10)
{
throw new MyException("Too low!);
}
//Blah, Blah, Blah...
}

The Throw clause can be used in any part of code where you feel a specific exception needs to be thrown to the calling method.
If a method is throwing an exception, it should either be surrounded by a try catch block to catch it or that method should have the throws clause in its signature. Without the throws clause in the signature the Java compiler does not know what to do with the exception. The throws clause tells the compiler that this particular exception would be handled by the calling method.

Is This Answer Correct ?    0 Yes 0 No

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

Answer / siva y

Throw is used to explicitly raise a exception within the program, the statement would be throw new Exception(); throws clause is used to indicate the exceptions that are not handled by the method. It must specify this behavior so the callers of the method can guard against the exceptions.

Throws is specified in the method signature. If multiple exceptions are not handled, then they are separated by a comma. the statement would be as follows: public void doSomething() throws IOException,MyException{}

Is This Answer Correct ?    0 Yes 0 No

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

Answer / ajay

Throws keyword suggests that the particular method contains
an Exception and that uncaught Exception is thrown to be
next class where it is supposed to handle(i.e.to be by next
user as he want to handle that exception in his own
way).Simply it throws uncaught exception.But Throws keyword
is used to throw only checked Exceptions and avoids compile
time errors.
And throw keyword,which is used in case of manual
Exception,throw object of corresponding exception from try
to catch just like java implicitely does in cases other than
manual Exception.

Is This Answer Correct ?    0 Yes 0 No

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

Answer / ishan gupta

Whenever We Handle the exception through THROW we used to write
a handler for handling that Exception that is "CATCH BLOCK".It is done Explicitly by The programmer
EX:-
public static void main(String[] args) {

int a=4;
int b=0;
int c=0;
try
{
c=a/b;
throw new ArithmeticException();
}
catch(ArithmeticException o){
System.out.println("Exception Catched");
}

}

Whenever We Handle the exception through THROWS than We are not worriyng about handling the Exception As this work is done by the JVM
Ex-

public static void main(String[] args)throws ArithmeticException {
int c=4/0;

}

Is This Answer Correct ?    0 Yes 0 No

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

Answer / balu

throws clause is used when the programmer does not want to handle the exception and throw it out of a method.
throw clause is used when the programmer wants to throw an exception explicitly and wants to handle it using catch block hence throw and throws are contradictory.
throw clause can be used to throw our own exceptions also i.e user-defined exceptions.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

Explain when classnotfoundexception will be raised ?

0 Answers  


Is java supports multiple inheritance? explain?

12 Answers   BUET, Inforica,


explain about jre and jvm

2 Answers  


What about method local inner classes or local inner classes in java?

0 Answers  


What classes of exceptions may be caught by a catch clause in java programming?

0 Answers  






Explain different types of wrapper classes in java?

0 Answers  


what is difference between class and object?

43 Answers   College School Exams Tests, HCL,


Realized?

0 Answers  


Is singleton set an interval?

0 Answers  


What are local variables?

0 Answers  


how to write a server program and sending the mails to the server using smtp protocol please help me

0 Answers   Lampex,


What are the different types of inner classes?

0 Answers  


Categories