Thursday, 16 April 2020

SE - 51 - Throw Vs Throws Vs Throwable !

As we saw in last blogs that the Throwable is parent Class of all the Exceptions/Errors.

Now we need to see difference between Throw and Throws :


Throws :
It is used to declare an exception to inform and to ensure the programmer that the exception might hit and it needs a handling. Throws is only for checked exceptions

  1. public void name() throws exception_class_name{ 
  2. //code
  3. }  
It is followed by a class.
It is given in method signature
We can give multiple exception in method signature



Throw :
It is used to explicitly throw an exception :

  1. throw new IOException("sorry device error);  
It is followed by instance of Exception class.
It is given in method body
There can be only 1 throw statement at once

No comments:

Post a Comment