Difference between throw and throws in Java
The throw and throws is the concept of exception handling where the throw keyword throw the exception explicitly from a method or a block of code whereas the throws keyword is used in signature of the method.
There are many differences between throw and throws keywords. A list of differences between throw and throws are given below:
||Basis of Differences
||Java throw keyword is used throw an exception explicitly in the code, inside the function or the block of code.
||Java throws keyword is used in the method signature to declare an exception which might be thrown by the function while the execution of the code.
||Type of exception Using throw keyword, we can only propagate unchecked exception i.e., the checked exception cannot be propagated using throw only.
||Using throws keyword, we can declare both checked and unchecked exceptions. However, the throws keyword can be used to propagate checked exceptions only.
||The throw keyword is followed by an instance of Exception to be thrown.
||The throws keyword is followed by class names of Exceptions to be thrown.
||throw is used within the method.
||throws is used with the method signature.
||We are allowed to throw only one exception at a time i.e. we cannot throw multiple exceptions.
||We can declare multiple exceptions using throws keyword that can be thrown by the method. For example, main() throws IOException, SQLException.
Java throw Example
Java throws Example
Java throw and throws Example