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
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 :
It is followed by instance of Exception class.
It is given in method body
There can be only 1 throw statement at once
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
- public void name() throws exception_class_name{
- //code
- }
It is given in method signature
We can give multiple exception in method signature
Throw :
It is used to explicitly throw an exception :
- throw new IOException("sorry device error);
It is given in method body
There can be only 1 throw statement at once
No comments:
Post a Comment