Java Throws Keyword
A low-level method that reads a file often can't sensibly handle a missing file: it doesn't know whether to ask the user, use a default, or give up. Its caller knows. The throws clause in a method's header says, "this method may throw these checked exceptions; whoever calls me must deal with them." It's a promise written into the method's signature, and the compiler holds every caller to it.
Passing Responsibility Up
import java.io.FileNotFoundException;
import java.io.FileReader;
public class Report {
static FileReader openMarks(String file) throws FileNotFoundException {
System.out.println("opening " + file);
return new FileReader(file);
}
static void printReport(String file) throws FileNotFoundException {
openMarks(file);
System.out.println("report printed");
}
public static void main(String[] args) {
try {
printReport("advitha-marks.txt");
} catch (FileNotFoundException e) {
System.out.println("No marks file yet, showing an empty report");
}
}
}opening advitha-marks.txt
No marks file yet, showing an empty reportHow it works
openMarksdeclaresthrows FileNotFoundException: it doesn't catch the problem, it passes it on.printReportcallsopenMarks, so it must also catch or declare. It declares too, passing the problem further up.mainis the place that knows what to do (show an empty report), so it catches it.
Catch or Declare?
| Catch it when... | Declare it when... |
|---|---|
| This method can do something useful: retry, use a default, show a message | Only the caller knows how to respond |
| The exception is an internal detail callers shouldn't see | The exception is part of the method's contract |
Rules of throws
| Rule | Example |
|---|---|
| List several types with commas | void save() throws IOException, SQLException |
| A parent type covers its children | throws IOException also covers FileNotFoundException |
| Unchecked exceptions may be listed, but don't have to be | throws IllegalArgumentException documents it |
main can declare too | public static void main(String[] args) throws IOException: the JVM prints the stack trace if it escapes |
| Overriding methods can't declare broader checked exceptions | See "Exception Handling with Method Overriding" |
Common Mistakes
throws Exception on every method. It hides real information and forces callers to catch everything.
Declaring and never handling anywhere. Somebody, eventually main, must decide what to do.
Thinking throws throws something. It only declares; throw actually throws.
Interview Corner
What is the throws keyword used for?
throws appears in a method or constructor declaration to list the checked exceptions it may propagate instead of handling. Callers must then catch those exceptions or declare them in turn. It can also list unchecked exceptions for documentation, although that isn't required.
Practice
- Easy: Write
static void pause(long ms) throws InterruptedExceptionthat callsThread.sleep(ms), and call it frommainwith a try-catch. - Harder: Add a third level to
Report.javabetweenprintReportandmain. Which methods needthrows, and which one catches?
Key Takeaways
Was this tutorial helpful?