Skip to main content

How to Declare a Checked Exception with throws in Java

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
static <type> <name>(<parameters>) throws <ExceptionType> {
...
}
PartWhat it does
throws <ExceptionType>Declares that the method can throw that checked exception. Callers must catch it or declare it too.
Several typesSeparate 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.java runs 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:

Main.java, where readNote reads a file with Files.readString and declares throws IOException, and main, which also declares throws IOException, writes note.txt, reads it, then reads missing.txt.

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​

Terminal
java Main.java
Output
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)

A zsh terminal where java Main.java prints Buy milk, then Exception in thread main java.nio.file.NoSuchFileException: missing.txt with its stack trace.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • The first readNote call read note.txt and printed Buy milk.
  • The second call threw NoSuchFileException, a kind of IOException. readNote didn't catch it, so it went up to main, which didn't catch it either, so Java printed it with the chain of calls that led there and stopped.
  • Without throws IOException on readNote, the file wouldn't even compile: error: unreported exception IOException; must be caught or declared to be thrown.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out throws on a method that calls Files.readStringerror: unreported exception IOException; must be caught or declared to be thrownDeclare it, or catch it in the method
Declaring throws Exception everywhereCallers can no longer see which failures to expectDeclare the specific types
Confusing throws with throwthrow raises an exception; throws only announces oneUse throw new ... to raise, throws ... in the signature

Troubleshooting​

SymptomLikely causeFix
unreported exception X; must be caught or declared to be thrownA call can throw the checked exception X and nothing handles itAdd throws X or a try/catch
A stack trace ends the programA declared exception reached main and main passed it onCatch 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​