A catch clause for a broad type such as Exception matches any more specific exception thrown inside the try block, whether that subtype is checked like IOException or unchecked like ArrayStoreException.
for (boolean throwChecked : new boolean[]{true, false}) {
try {
if (throwChecked) {
throw new java.io.IOException("checked failure");
}
Object[] numbers = new Integer[2];
numbers[0] = "not an integer";
} catch (Exception e) {
System.out.println("Caught as Exception: " + e.getClass().getSimpleName() + ": " + e.getMessage());
}
}
Caught as Exception: IOException: checked failure
Caught as Exception: ArrayStoreException: java.lang.String
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-09-27