Below is the list of difference between throw and throws:
Point of Distinction | throw | throws |
---|---|---|
Exception Handling | Used to throw an exception explicitly. | Declare an exception |
How used with Methods | Used in method implementation | Used in method signature |
No of exception handled | We can throw only 1 exception at a time. | We can handle multiple exceptions by declaring them using throws keyword. |
Handle which type of exception | Used to handle user defined exception | Used to handle checked exception |
Followed By | throw is followed by instance. | throws is followed by class. |
Example:
throw example:
void XYZ()
{
if(array==null)
{
throw new NullPointerException(...);
}
throws example:
public int myMethod() throws IOException, ArithmeticException
{
}
Comments