Junior — Middle
Can you explain what a checked exception is in programming languages?
sobes.tech AI
Answer from AI
A checked exception is a type of exception in Java that the compiler requires to be either handled (try-catch) or declared in the method signature using the throws keyword. They are intended for situations that may occur during runtime but must be anticipated and handled by the programmer, such as input-output errors (IOException).
Example:
public void readFile(String path) throws IOException {
FileReader reader = new FileReader(path);
// ...
}
If a method calls another method that throws a checked exception, it must either handle the exception or declare it further. This helps make the code more reliable and explicit regarding potential errors.