Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 840
tttppp Avatar asked Jun 16 '11 08:06

tttppp


People also ask

Why it is called as checked exception?

In broad terms, a checked exception (also called a logical exception) in Java is something that has gone wrong in your code and is potentially recoverable. For example, if there's a client error when calling another API, we could retry from that exception and see if the API is back up and running the second time.

Why there is a category of checked exceptions?

Checked exception Checked exceptions are also known as compile-time exceptions as these exceptions are checked by the compiler during the compilation process to confirm whether the exception is handled by the programmer or not. If not, then the system displays a compilation error.

How do I fix Java Lang InstantiationException?

How to Resolve InstantiationException. To avoid the InstantiationException , it should be ensured that the instance of the class that is attempted to be created at runtime using Class. newInstance() is a concrete class and not an abstract class, interface, array class, primitive or void.

Which exception is a checked exception?

Checked Exception − A checked exception is an exception that occurs at the time of compilation, these are also called as compile time exceptions. These exceptions cannot simply be ignored at the time of compilation; the programmer should take care of (handle) these exceptions.


2 Answers

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.

like image 64
Lukas Eder Avatar answered Sep 24 '22 06:09

Lukas Eder


From the JavaDoc for InstantiationException:

Thrown when an application tries to create an instance of a class using the newInstance method in class Class, but the specified class object cannot be instantiated because it is an interface or is an abstract class.

This will only happen when using Java reflection, e.g. when programmatically instantiating objects, e.g. ClassName.class.newInstance() as opposed to new ClassName() so to speak. It is only natural to expect whoever uses reflection to write code that handles any such aberrations like instantiating an abstract class or an interface or if there is an exception thrown in during the constructor invocation (in which case you can use e.getCause()).

It is not expected to be handled in your code -- but rather by that particular API/library that uses reflection.

like image 38
Nick Avatar answered Sep 24 '22 06:09

Nick