Friday, March 13, 2015

InstantiationException

java.lang.InstantiationException

InstantiationException is described in the javadoc comments as:

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.
author: unascribed version: 1.17, 12/19/03 see: java.lang.Class#newInstance() since: JDK1.0

Where is this exception thrown?

Following, is a list of exception messages cross-referenced to the source code responsible for throwing them. Click on the method link to view the code and see how the exception is thrown.

How is this exception thrown?

The following sub-sections identify where this exception is thrown, and how (or why) the code is throwing the exception.

Any source code quoted in this section is subject to the Java Research License unless stated otherwise.

java.lang.Class.newInstance0()

private T newInstance0() throws InstantiationException, IllegalAccessException {
    if (cachedConstructor == null) {
        if (this == Class.class) {
            throw new IllegalAccessException('Can not call newInstance() on the Class for java.lang.Class');
        }
        try {
            Class[] empty = {};
            final Constructor<T> c = getConstructor0(empty, Member.DECLARED);
            java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {

                public Object run() {
                    c.setAccessible(true);
                    return null;
                }
            });
            cachedConstructor = c;
        } catch (NoSuchMethodException e) {
            throw new InstantiationException(getName());
        }
    }
    Constructor<T> tmpConstructor = cachedConstructor;
    int modifiers = tmpConstructor.getModifiers();
    if (!Reflection.quickCheckMemberAccess(this, modifiers)) {
        Class caller = Reflection.getCallerClass(3);
        if (newInstanceCallerCache != caller) {
            Reflection.ensureMemberAccess(caller, this, null, modifiers);
            newInstanceCallerCache = caller;
        }
    }
    try {
        return tmpConstructor.newInstance((Object[]) null);
    } catch (InvocationTargetException e) {
        Unsafe.getUnsafe().throwException(e.getTargetException());
        return null;
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

No comments:

Post a Comment