Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6850612

Deprecate Class.newInstance since it violates the checked exception language contract

    XMLWordPrintable

Details

    Description

      A DESCRIPTION OF THE REQUEST :
      Please, deprecate Class.newInstance in JDK7 and add alternative method newObjectInstance which will declare exception correctly.
      (or by wrapping exception or by adding generic throws clause)


      JUSTIFICATION :
      Class.newInstance violate checked exception definition.
      It's' allows undeclared checked excetion to be actually throwed, which is in contradiction with rules of language and cause errors.

        See also comments to
        See also http://bugs.sun.com/view_bug.do?bug_id=5066799

      related bugs:
      http://bugs.sun.com/view_bug.do?bug_id=6195208
      http://bugs.sun.com/view_bug.do?bug_id=4233093

      they 'fixed' by specifing fundamentally incorrect behaviour in documentation, which is not fix of problem.


      ---------- BEGIN SOURCE ----------
      next code frtagment

      >>public class X
      >> {
      >>
      >> public X() throws Exception
      >> {
      >> throw new Exception("hi!");
      >> }
      >>
      >> public static void f() {
      >> try {
      >> X x = X.class.newInstance();
      >> } catch (InstantiationException ex) {
      >> System.err.println("instantiation exception");
      >> ex.printStackTrace();
      >> } catch (IllegalAccessException ex) {
      >> System.err.println("illegal access exception");
      >> ex.printStackTrace();
      >> }
      >> }
      >>
      >> }
      >>

      will throw checked exception X from function X.f(), which is not declare one.
      ---------- END SOURCE ----------

      Attachments

        Issue Links

          Activity

            People

              darcy Joe Darcy
              ndcosta Nelson Dcosta (Inactive)
              Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: