~/ learn/ java-from-zero/ cards/ throw vs throws: raising and declaring
1 of 4

Type a method that throws and declares a checked exception

Type a method that throws and declares a checked exception

Answer

public String load(String path) throws IOException { return Files.readString(Path.of(path)); }

IOException is checked, so the method must declare throws IOException. Without it, the compiler rejects callers that don't handle it.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/e17f6566-5fd0-4dc0-a6b0-8b23459362fb/flashcard utf-8 LF