Java throws Keyword

Last Updated : 13 Jan 2026

The throws keyword in Java is used in a method declaration to specify that the method may pass one or more exceptions to the calling method. In this chapter, we will learn how the throws keyword works, when it should be used, how it differs from the throw keyword, and how it helps in handling checked exceptions effectively in Java programs.

What is throws Keyword in Java?

The throws keyword is a keyword that is used to declare possible exceptions that a method can generate and allow those exceptions to be handled by the method that invokes it rather than within the method itself.

Syntax

The syntax of throws keyword for single exception is as follows:

The syntax of throws keyword for multiple exceptions is as follows:

Examples of throws Keyword

Here are examples of the throws keyword for single and multiple exceptions.

Example 1: Throws with Single Exception

The following example demonstrates the throws keyword with a single exception:

Compile and Run

Output:

Exception caught: Not eligible to vote

Example 2: Throws with Multiple Exceptions

The following example demonstrates the throws keyword with multiple exceptions:

Output:

File not found: test.txt (No such file or directory)

Rule: If we are calling a method that declares an exception, we must either catch or declare the exception.

When to Use the throws Keyword?

The throws keyword is used when a method might cause a checked exception but does not handle it itself. It tells the caller that the method can throw an exception, so the caller can decide how to handle it.

It is commonly used in methods that work with files, databases, or network operations that helps to keep the code clean by passing the responsibility of handling exceptions to higher-level methods.

Handling or Declaring Exceptions

If we are calling a method that declares an exception, we must either catch the exception or declare it using the throws keyword.

There are two cases:

  1. Handle the exception using a try-catch block
  2. Declare the exception using the throws keyword

Case 1: Handle Exception Using try-catch

When we handle the exception, the program executes normally whether the exception occurs or not.

The following example demonstrates handling an exception using a try-catch block:

Compile and Run

Output:

exception handled
normal flow...

Case 2: Declare Exception Using throws

If we declare the exception and it does not occur, the program runs normally. If the exception occurs, it is thrown at runtime, because throws only declare the exception - it does not handle it.

A) Exception Does Not Occur

The following example demonstrates declaring an exception using the throws keyword when no exception occurs:

Compile and Run

Output:

Device operation performed
normal flow...

B) Exception Occurs

The following example demonstrates declaring an exception using the throws keyword when the exception occurs at runtime:

When we run the above code, we get the following exception

Exception in thread "main" java.io.IOException: device error
	at M.method(Main.java:4)
	at Main.main(Main.java:10)

Next TopicThrow vs Throws