what is difference between throw and throws in exception?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are basic keywords?

559


What is concurrent hashmap and its features?

528


What are the five major types of reference sources?

543


Can a static class have a constructor?

523


What is OOP Language?

631






How do I enable java in safari?

550


Explain the difference between abstraction and encapsulation.

539


What is exception handling in java?

576


What are instance variables?

616


What is multithreading in java?

552


What are the high-level thread states in java programming?

589


Java.util.regex consists of which classes?

548


What is the difference between a static and a non-static inner class in java programming?

533


why java uses class level type casting ?

2253


What is string and example?

559