Search code examples
javaexceptioninstantiationexception

Why is InstantiationException a checked exception?


My understanding is that checked exceptions are those that the caller of the can reasonably be expected to recover from. I don't understand why this is the case with InstantiationException. If a class cannot be instantiated then what is the caller expected to do?

I then thought that maybe it was an important consideration that the code had compiled - therefore this could only happen if a class is dynamically specified.1 In this case the class may be more like a parameter, but then we have IllegalArgumentException that is a runtime exception.

What is the rational behind which standard exceptions are checked, and which are not?

1 Is this true?


Solution

  • One reason for explicitly handling this exception that I can think of (but that's not an authoritative answer):

    Try instanciating a class with reflection (because that class is configured, not statically linked). If it doesn't have the expected constructor signature, try another constructor. Or another class. Any framework code (such as Spring) might have such logic.