Whats the difference between throw and throws keyword?
Printable View
Whats the difference between throw and throws keyword?
when using throws keyword you are telling the JVM that the exception that if occurred in the current method should be sent to the calling method and the caller must handle the exception.
on the other hand throw keyword allows you to manually generate objects of Exception or its Subclasses and throw them back to the caller.
Using throw you can generate your custom exceptions(user defined) and send them back to caller
they are used as :
Code:public void someMethod throws MyException{
if(condition){
throw new MyException();
}
}
The throws keyword use in the method declaration to tell that the method throws some type of exception. While the throw keyword is used to throw the actual exception from a method.
Thanks