Now, i am going to describe about java throw and throws keyword:-
Throw and Throws keywords are used to handle Exception feature in Java programming language. As we know that, Exception handling is an important part in Java. Which enables you to write robust examples or programs or applications. In java, there are five keywords to handle Exception in Java like:- try, catch, finally, throw and throws
There are following difference between throw and throws in Java:-
- throw is a keyword, which is used to throw an Exception from any method or static block and throws is also a keyword, which is used in method declaration, denoted which Exception can possible be thrown by this method.
- throw keyword can be used in switch case but throws keyword can not be used anywhere except on method declaration.
- throw returns or transfers control to caller but throws is used to suggest information and compiler checking.
- In java, as per rules of overriding, overriding method can not throw Checked Exception higher in hierarchy than overridden method . This is rules for throws clause while overriding method.
- If any method throws checked Exception than caller can either handle this exception by catching it or can re throw it by declaring another throws clause in method declaration.