< prev index next >

src/java.base/share/classes/java/lang/reflect/UndeclaredThrowableException.java

Print this page




  45  * handler, and it can be retrieved with the
  46  * {@code getUndeclaredThrowable()} method.
  47  * {@code UndeclaredThrowableException} extends
  48  * {@code RuntimeException}, so it is an unchecked exception
  49  * that wraps a checked exception.
  50  *
  51  * <p>As of release 1.4, this exception has been retrofitted to
  52  * conform to the general purpose exception-chaining mechanism.  The
  53  * "undeclared checked exception that was thrown by the invocation
  54  * handler" that may be provided at construction time and accessed via
  55  * the {@link #getUndeclaredThrowable()} method is now known as the
  56  * <i>cause</i>, and may be accessed via the {@link
  57  * Throwable#getCause()} method, as well as the aforementioned "legacy
  58  * method."
  59  *
  60  * @author      Peter Jones
  61  * @see         InvocationHandler
  62  * @since       1.3
  63  */
  64 public class UndeclaredThrowableException extends RuntimeException {

  65     static final long serialVersionUID = 330127114055056639L;
  66 
  67     /**
  68      * Constructs an {@code UndeclaredThrowableException} with the
  69      * specified {@code Throwable}.
  70      *
  71      * @param   undeclaredThrowable the undeclared checked exception
  72      *          that was thrown
  73      */
  74     public UndeclaredThrowableException(Throwable undeclaredThrowable) {
  75         super(null, undeclaredThrowable);  // Disallow initCause
  76     }
  77 
  78     /**
  79      * Constructs an {@code UndeclaredThrowableException} with the
  80      * specified {@code Throwable} and a detail message.
  81      *
  82      * @param   undeclaredThrowable the undeclared checked exception
  83      *          that was thrown
  84      * @param   s the detail message


  91 
  92     /**
  93      * Returns the {@code Throwable} instance wrapped in this
  94      * {@code UndeclaredThrowableException}, which may be {@code null}.
  95      *
  96      * <p>This method predates the general-purpose exception chaining facility.
  97      * The {@link Throwable#getCause()} method is now the preferred means of
  98      * obtaining this information.
  99      *
 100      * @return the undeclared checked exception that was thrown
 101      */
 102     public Throwable getUndeclaredThrowable() {
 103         return super.getCause();
 104     }
 105 
 106     /**
 107      * Serializable fields for UndeclaredThrowableException.
 108      *
 109      * @serialField undeclaredThrowable Throwable
 110      */

 111     private static final ObjectStreamField[] serialPersistentFields = {
 112         new ObjectStreamField("undeclaredThrowable", Throwable.class)
 113     };
 114 
 115     /*
 116      * Reconstitutes the UndeclaredThrowableException instance from a stream
 117      * and initialize the cause properly when deserializing from an older
 118      * version.
 119      */

 120     private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
 121         ObjectInputStream.GetField fields = s.readFields();
 122         Throwable exception = (Throwable) fields.get("undeclaredThrowable", null);
 123         if (exception != null) {
 124             SharedSecrets.getJavaLangAccess().setCause(this, exception);
 125         }
 126     }
 127 
 128     /*
 129      * To maintain compatibility with older implementation, write a serial
 130      * "ex" field with the cause as the value.
 131      */

 132     private void writeObject(ObjectOutputStream out) throws IOException {
 133         ObjectOutputStream.PutField fields = out.putFields();
 134         fields.put("undeclaredThrowable", super.getCause());
 135         out.writeFields();
 136     }
 137 }


  45  * handler, and it can be retrieved with the
  46  * {@code getUndeclaredThrowable()} method.
  47  * {@code UndeclaredThrowableException} extends
  48  * {@code RuntimeException}, so it is an unchecked exception
  49  * that wraps a checked exception.
  50  *
  51  * <p>As of release 1.4, this exception has been retrofitted to
  52  * conform to the general purpose exception-chaining mechanism.  The
  53  * "undeclared checked exception that was thrown by the invocation
  54  * handler" that may be provided at construction time and accessed via
  55  * the {@link #getUndeclaredThrowable()} method is now known as the
  56  * <i>cause</i>, and may be accessed via the {@link
  57  * Throwable#getCause()} method, as well as the aforementioned "legacy
  58  * method."
  59  *
  60  * @author      Peter Jones
  61  * @see         InvocationHandler
  62  * @since       1.3
  63  */
  64 public class UndeclaredThrowableException extends RuntimeException {
  65     @java.io.Serial
  66     static final long serialVersionUID = 330127114055056639L;
  67 
  68     /**
  69      * Constructs an {@code UndeclaredThrowableException} with the
  70      * specified {@code Throwable}.
  71      *
  72      * @param   undeclaredThrowable the undeclared checked exception
  73      *          that was thrown
  74      */
  75     public UndeclaredThrowableException(Throwable undeclaredThrowable) {
  76         super(null, undeclaredThrowable);  // Disallow initCause
  77     }
  78 
  79     /**
  80      * Constructs an {@code UndeclaredThrowableException} with the
  81      * specified {@code Throwable} and a detail message.
  82      *
  83      * @param   undeclaredThrowable the undeclared checked exception
  84      *          that was thrown
  85      * @param   s the detail message


  92 
  93     /**
  94      * Returns the {@code Throwable} instance wrapped in this
  95      * {@code UndeclaredThrowableException}, which may be {@code null}.
  96      *
  97      * <p>This method predates the general-purpose exception chaining facility.
  98      * The {@link Throwable#getCause()} method is now the preferred means of
  99      * obtaining this information.
 100      *
 101      * @return the undeclared checked exception that was thrown
 102      */
 103     public Throwable getUndeclaredThrowable() {
 104         return super.getCause();
 105     }
 106 
 107     /**
 108      * Serializable fields for UndeclaredThrowableException.
 109      *
 110      * @serialField undeclaredThrowable Throwable
 111      */
 112     @java.io.Serial
 113     private static final ObjectStreamField[] serialPersistentFields = {
 114         new ObjectStreamField("undeclaredThrowable", Throwable.class)
 115     };
 116 
 117     /*
 118      * Reconstitutes the UndeclaredThrowableException instance from a stream
 119      * and initialize the cause properly when deserializing from an older
 120      * version.
 121      */
 122     @java.io.Serial
 123     private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
 124         ObjectInputStream.GetField fields = s.readFields();
 125         Throwable exception = (Throwable) fields.get("undeclaredThrowable", null);
 126         if (exception != null) {
 127             SharedSecrets.getJavaLangAccess().setCause(this, exception);
 128         }
 129     }
 130 
 131     /*
 132      * To maintain compatibility with older implementation, write a serial
 133      * "ex" field with the cause as the value.
 134      */
 135     @java.io.Serial
 136     private void writeObject(ObjectOutputStream out) throws IOException {
 137         ObjectOutputStream.PutField fields = out.putFields();
 138         fields.put("undeclaredThrowable", super.getCause());
 139         out.writeFields();
 140     }
 141 }
< prev index next >