1. throws keyword is used to declare an exception with the method name. It works like the try-catch block because the caller needs to handle the exception thrown by throws. On the other hand, throw keyword is used to throw an exception explicitly.
2. throw is followed by an instance of Exception class and throws is followed by exception class names.
throw
throw new ArithmeticException("Arithmetic Exception");throws
throws ArithmeticException;
3. throw keyword is used within the body of a method, On the other hand throws is used in method signature to declare the exceptions.
throw:
void usingThrowKeyword()
{
try
{
//throwing arithmetic exception using throw
throw new ArithmeticException("You can’t divide a number by zero.");
}
catch (ArithmeticException e)
{
// handling exception
}
}throws:
//arithmetic exception using throws
void usingThrowsKeyword() throws ArithmeticException
{
//Statements
}4. By using throw keyword you can declare only one exception at a time, but you can handle multiple exceptions by declaring them using throws keyword.
throw:
void usingThrowKeyword()
{
//By using of throw keyword single exception is throwing
throw new ArithmeticException("You can’t divide a number by zero.");
}throws:
//By using of throws declaring multiple exceptions
void myMethod() throws ArithmeticException, NullPointerException
{
// handling exception
}
