Throws in exception handling in java

Throws keyword in java is used to declare exceptions that can be thrown by the method. Throws keyword can be used to throw multiple exception.

syntax:

1. Throws single exception

example:

2. Throws multiple exception

example:

 

When a method throws an exception, the code that uses this method needs to handle this exception.
Handling of exception can be of 2 types
1. Use a Try-Catch block
2. Use throws method to again throw the same or new exception.

Lets see an example where we have a method convertListToString that throws exception.
In main method we call the convertListToString and also handle the exception that was thrown.

Example : Throws in exception handling

Output

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn