Summary: In this tutorial, you will learn how to use the Java throws clause to specify the exceptions that may be thrown by a method.
Introduction to the Java throws clause
Suppose you define a method that may throw a checked exception e.g., IOException
. To make the code valid, you need to handle the exception using the try catch statement.
However, you may not want to handle the exceptions inside the method. Instead, you want to propagate the exceptions up to the call stack, allowing the caller of the method to handle it.
To do that, you can declare the exceptions that you want to propagate in the throws
clause of the method declaration.
In Java, the throws
clause specifies that the method may throw one or more specific types of exceptions. Here’s the basic syntax of a method with a throws
clause:
returnType methodName(parameters) throws ExceptionType1, ExceptionType2, ... {
// method implementation
}
Code language: Java (java)
In this syntax, you specify one or more exception types that the method may throw after the throws
keyword.
Let’s take an example to understand how Java throws
clause works.
Java throws clause example
The following program reads a text file and writes each line to the screen:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
public class App {
public static void readFile(String filename) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.err.println(e.getMessage());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
}
public static void main(String[] args) {
readFile("src/readme.txt");
}
}
Code language: Java (java)
In this example, we handle the IOException
using a try catch finally statement inside the readFile()
method.
If you don’t want to handle the IOException
in the readFile
method but in the main()
method, you can do these steps:
- First, declare the
IOException
in thereadFile()
method signature and remove thetry catch finally
statement from the method. - Second, surround the
readFile()
method with thetry catch
statement call in themain()
method.
Here’s the new program:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
public class App {
public static void readFile(String filename) throws IOException {
var reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
}
public static void main(String[] args) {
try {
readFile("src/readme.txt");
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
Code language: Java (java)
The program works fine. But if an exception occurs while reading the file, the BufferReader
will not be closed properly.
To ensure the BufferedReader
is properly closed even if an exception occurs, you can use the try-with-resources
block:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
public class App {
public static void readFile(String filename) throws IOException {
try (var reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
public static void main(String[] args) {
try {
readFile("src/readme.txt");
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
Code language: Java (java)
The try-with-resources
block automatically closes the BufferedReader
when it’s no longer needed regardless of whether an exception occurs or not.
Summary
- Use the Java
throws
clause to specify one or more exceptions that a method has and propagate them up to the call stack, allowing its caller to handle them.