Junior — Middle
Could you explain what checked exceptions are in a programming language?
sobes.tech AI
Answer from AI
In Java, checked exceptions are exceptions that are checked by the compiler at compile time. If a method can throw a checked exception, it must either handle it internally (try-catch) or declare it in the method signature using the throws keyword. This forces the programmer to explicitly consider the possibility of such errors, for example, when working with files or network operations.
Example:
public void readFile(String path) throws IOException {
FileReader file = new FileReader(path);
// ...
}
Here, IOException is a checked exception, and the method declares that it may throw it.