If a method does not handle a checked exception, the method must declare it using the throwskeyword. The throws keyword appears at the end of a method's signature.
You can throw an exception, either a newly instantiated one or an exception that you just caught, by using the throw keyword. Try to understand the different in throws and throw keywords.
Example (Throw)
// Throw
// Throw statement crates and throws an exception
class ex4
{
static void excall()
{
try
{
throw new NullPointerException(" Function call");
// new is used to construct an instance of NullPointerException .
}
catch (NullPointerException e)
{
System.out.println("Inside catch of excall function");
}
}
public static void main(String args[])
{
try
{
excall();
}
catch(NullPointerException e)
{
System.out.println("Recaught ");
}
}
}
Output
