javaexceptionruntimeunchecked-exception

Check if an exception is a checked exception at runtime


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()));
}

Solution

  • I guess some condition like below will be enough

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