try { // some code that may throw an IOException } catch (IOException e) { if (e.getClass() == FileNotFoundException.class) { // handle file not found case } else { // handle other IO errors } }
try { // some code that may throw an OutOfMemoryError } catch (Throwable t) { if (t.getClass().equals(OutOfMemoryError.class)) { // handle out of memory case } else { // handle other errors and exceptions } }In this example, we are catching any `Throwable` (including `Errors`) and checking if it is an `OutOfMemoryError`. The `Throwable` class is part of the `java.lang` package, which is a core Java library.