Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

what is difference between throw and throws in exception?

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What’s the difference between the methods sleep() and wait()?

1010


What is static and final keyword in java?

1019


Say any two properties in beans?

1060


Is java same as core java?

1091


Which is bigger float or double java?

977


What is an interface in java? Explain

1178


Explain the difference between abstract class and interface in java?

981


What is sizeof () operator?

971


What are facelets templates?

1031


What is compareto () in java?

972


Can one thread block the other thread?

1028


Why hashset is used in java?

1067


Is it possible to write a regular expression to check if string is a number?

1026


What is scope & storage allocation of global and extern variables? Explain with an example

1038


Write a program to print fibonacci series up to count 10.

900