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

Is alive method in java?

504


What is a website container?

503


Explain jdk, jre and jvm?

567


Why does my function print none?

516


In how many ways we can do synchronization in java?

523






What does regex mean?

564


What is a nullable field?

575


What is balanced tree in java?

532


What are the important features of Java 11 release?

576


what is static import in java? Explain

551


What is stringjoiner ?

573


What is a method in coding?

566


When a thread is executing synchronized methods , then is it possible to execute other synchronized methods simultaneously by other threads?

567


What will happen when using pass by reference in java?

520


Do we have pointers in java?

524