what is difference between throw and throws in exception?

Answer Posted / raj

throws --> throws declare that it throws the exception to
the calling method.

throw --> it is surrounded by the try catch block actually
it does the work, of throwing the exception.

For Example:

public void testMethod() throws BusinessException
{
try
{
}
catch(Exception exe)
{
// catch all the exceptions and provide some meaningful
message
throw BusinessException("An error occured while
trying to connect to DB");
}

}

Is This Answer Correct ?    43 Yes 13 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What causes memory leak in java?

702


What is difference between public static and void?

703


Is java programming easy?

746


What is a lightweight component?

802


What restrictions are placed on method overriding in java programming?

768


Explain the term virtual machine?

810


Can a private method be declared as static?

789


Is math class static in java?

842


What are the files generated after using IDL to java compiler?

822


How can you read an integer value from the keyword when the application is runtime in java? example?

869


Explain static nested classes ?

821


What is method in research paper?

781


Does java support multiple inheritance or not?

817


What are the parts of a method?

759


Is 0 true or false in java?

762