What is the difference between throw and throws?

Answer Posted / rakesh nath

Throw clause is used in any part of the code where you want
to throw any specific exception to the calling method while
throws can be termed as a substitute for try-catch block.

When an exception occurs in a program, the compiler needs to
know what should be done with that exception. For that
either we should have a try-catch block or the method should
have the throws clause attached, as follows.

<access-modifier> <return-type> myMethod(<parameter-list>)
throws <exception-list>

for example, public void meth1() throws MyException

throw can handle only ONE throwable object while throws can
handle multiple throwable objects seperated by commas.

Is This Answer Correct ?    13 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why java does not support pointers?

563


Can we overload run() method in java?

599


What is java reflection?

541


What is static synchronization?

581


How can you write a loop indefinitely in java programming?

556






how to create multithreaded program? : Java thread

512


How does linkedlist work in java?

510


What does method mean?

521


how many types of Inheritance?

657


Can I overload to string method

972


Why is java not 100% pure oops?

623


How many types of memory areas are allocated by JVM in java?

599


What is the difference between static class and normal class?

494


Is java type safe?

515


What is an escape character in java?

538