Check if an exception is a checked exception at runtime

1.4k Views Asked by At

Is this the correct way to check if an exception is a checked exception at runtime?

public boolean isChecked(final Throwable e) {
    return !(RuntimeException.class.isAssignableFrom(e.getClass()) 
             || Error.class.isAssignableFrom(e.getClass()));
}
1

There are 1 best solutions below

4
Steyrix On BEST ANSWER

I guess some condition like below will be enough

return !(e instanceof RuntimeException || e instanceof Error);