~/ 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/e5b5d7b5-0da2-43fc-a3dc-0d88be881a61/flashcard utf-8 LF