Exception Propagation

Throws Keyword in Java with Example

What is throws keyword in Java?

throws keyword is used to declare that a method may throw one or some exceptions. The caller must catch the exceptions.

Suppose in your java program you using a library method which throws an Exception
 

In your program, you will handle this exception using try & catch.

Example:

throw new IOException(“can not open connection”);

Example:

throws IOException, ArrayIndexBoundException;

Summary

The “Java throw keyword” is used to declare an exception. For any method that will “throw” an exception, it is mandatory that in the calling method, you use throws to list the exception thrown.

top
X
Glad to have you at Our Website
Welcome to WPBot
wpChatIcon