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

Throwables propagate out of reflective invocations

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2 P2
    • 1.4.0
    • 1.4.0
    • core-libs
    • None

      Peter.Jones@East pointed out that only Errors should be allowed to propagate out
      of reflective invocations; arbitrary Throwables should be wrapped in
      InvocationTargetException. This is a change in behavior due to the reflection
      rewrite that was put back in build 51; see also bug 4417941, which is related.

      Test case from above author:

      import java.lang.reflect.*;

      public class TestThrowable {

          public static void main(String[] args) {
      try {
      Class c = TestThrowable.class;
      Method tt = c.getDeclaredMethod("throwThrowable", null);
      Method te = c.getDeclaredMethod("throwError", null);

      try {
      System.out.println("invoking throwThrowable():");
      tt.invoke(null, null);
      } catch (InvocationTargetException e) {
      System.out.println(
      "--> caught InvocationTargetException containing: " +
      e.getTargetException());
      } catch (Throwable t) {
      System.out.println("--> caught Throwable directly: " + t);
      }

      try {
      System.out.println("invoking throwError():");
      te.invoke(null, null);
      } catch (InvocationTargetException e) {
      System.out.println(
      "--> caught InvocationTargetException containing: " +
      e.getTargetException());
      } catch (Throwable t) {
      System.out.println("--> caught Throwable directly: " + t);
      }

      } catch (Exception e) {
      System.err.println("--> unexpected exception:");
      e.printStackTrace();
      }
          }

          static void throwThrowable() throws Throwable {
      throw new Throwable();
          }

          static void throwError() {
      throw new Error();
          }
      }


            kbr Kenneth Russell (Inactive)
            kbr Kenneth Russell (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: