i am trying to define a list of exception classes like so:
private static final List<Class<? extends Exception>> SOME_ERRORS = Arrays.asList(NumberFormatException.class, NullPointerException.class);
the error i get from Eclipse is this:
Type mismatch: cannot convert from List<Class<? extends RuntimeException>> to List<Class<? extends Exception>>
Could you please advise? I don’t see why it can’t convert a list of Exceptions to a list of Exceptions…
+1 for Reimeus’s answer. However, if you’d prefer to keep the list declared using the parent
Exceptiontype, this is the best I can come up with (using Java 7 syntax):Your issue is due to
Arrays.asListautomatically determining the proper list type for you. To compare, this also works without error:Especially dealing with “constants” (
static finals), you’re usually best advised to ensure that such arrays / collections can’t be modified. At worst, this can save running into some difficult issues. For example: