How to Declare a Checked Exception with throws in Java
Java makes you deal with checked exceptions, such as IOException: a method that can cause one must either catch it or declare it with throws. Declaring it passes the exception up to whoever calls the method, and from main up to the terminal.
Syntax
static <type> <name>(<parameters>) throws <ExceptionType> {
...
}
| Part | What it does |
|---|---|
throws <ExceptionType> | Declares that the method can throw that checked exception. Callers must catch it or declare it too. |
| Several types | Separate them with commas: throws IOException, InterruptedException. |
The Java Language Specification, section 11.1.1: "The checked exception classes are all exception classes other than the unchecked exception classes. That is, the checked exception classes are Throwable and all its subclasses other than RuntimeException and its subclasses and Error and its subclasses."
The Example
The example has a readNote method that reads a file with Files.readString, which can throw IOException, so the method declares throws IOException, and so does main, which calls it. main writes a note, reads it back, then reads a file that doesn't exist.
Prerequisites
- A JDK, the Java Development Kit.
java Main.javaruns a single file without compiling it first. This guide uses OpenJDK 25.0.5.1.
Step 1: Write the Code
Create a file named Main.java:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
static String readNote(Path path) throws IOException {
return Files.readString(path);
}
public static void main(String[] args) throws IOException {
Files.writeString(Path.of("note.txt"), "Buy milk");
System.out.println(readNote(Path.of("note.txt")));
System.out.println(readNote(Path.of("missing.txt")));
}
}
Step 2: Run It
java Main.java
Buy milk
Exception in thread "main" java.nio.file.NoSuchFileException: missing.txt
at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:106)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
at java.base/sun.nio.fs.UnixFileSystemProvider.newFileChannel(UnixFileSystemProvider.java:213)
at java.base/sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:244)
at java.base/java.nio.file.Files.newByteChannel(Files.java:357)
at java.base/java.nio.file.Files.newByteChannel(Files.java:399)
at java.base/java.nio.file.Files.readAllBytes(Files.java:2973)
at java.base/java.nio.file.Files.readString(Files.java:3043)
at java.base/java.nio.file.Files.readString(Files.java:3006)
at Main.readNote(Main.java:7)
at Main.main(Main.java:13)

The command is the same in zsh, bash and PowerShell.
What Happened
- The first
readNotecall readnote.txtand printedBuy milk. - The second call threw
NoSuchFileException, a kind ofIOException.readNotedidn't catch it, so it went up tomain, which didn't catch it either, so Java printed it with the chain of calls that led there and stopped. - Without
throws IOExceptiononreadNote, the file wouldn't even compile:error: unreported exception IOException; must be caught or declared to be thrown.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out throws on a method that calls Files.readString | error: unreported exception IOException; must be caught or declared to be thrown | Declare it, or catch it in the method |
Declaring throws Exception everywhere | Callers can no longer see which failures to expect | Declare the specific types |
Confusing throws with throw | throw raises an exception; throws only announces one | Use throw new ... to raise, throws ... in the signature |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
unreported exception X; must be caught or declared to be thrown | A call can throw the checked exception X and nothing handles it | Add throws X or a try/catch |
| A stack trace ends the program | A declared exception reached main and main passed it on | Catch it where you can do something useful |
FAQ
Do I need throws for every exception?
No. Only checked exceptions. Unchecked ones, RuntimeException and its subclasses such as IllegalArgumentException, need no declaration.
Should main declare throws?
For a small program, it's a simple way to let a failure stop it with a clear message. In larger programs, catch the exception where you can report it properly.
Conclusion
throws IOException in a method's signature tells Java, and every caller, that the method can fail with that checked exception. Each caller then catches it or declares it in turn, all the way up to main.
Additional Resources
- Chapter 11. Exceptions, in the Java Language Specification.
