< prev index next >

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

Print this page

      1 /*
      2  * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
      3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
      4  *
      5  * This code is free software; you can redistribute it and/or modify it
      6  * under the terms of the GNU General Public License version 2 only, as
      7  * published by the Free Software Foundation.  Oracle designates this
      8  * particular file as subject to the "Classpath" exception as provided
      9  * by Oracle in the LICENSE file that accompanied this code.
     10  *
     11  * This code is distributed in the hope that it will be useful, but WITHOUT
     12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     14  * version 2 for more details (a copy is included in the LICENSE file that
     15  * accompanied this code).
     16  *
     17  * You should have received a copy of the GNU General Public License version
     18  * 2 along with this work; if not, write to the Free Software Foundation,
     19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     20  *
     21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     22  * or visit www.oracle.com if you need additional information or have any
     90         super(s, undeclaredThrowable);  // Disallow initCause
     91     }
     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 }
      1 /*
      2  * Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
      3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
      4  *
      5  * This code is free software; you can redistribute it and/or modify it
      6  * under the terms of the GNU General Public License version 2 only, as
      7  * published by the Free Software Foundation.  Oracle designates this
      8  * particular file as subject to the "Classpath" exception as provided
      9  * by Oracle in the LICENSE file that accompanied this code.
     10  *
     11  * This code is distributed in the hope that it will be useful, but WITHOUT
     12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     14  * version 2 for more details (a copy is included in the LICENSE file that
     15  * accompanied this code).
     16  *
     17  * You should have received a copy of the GNU General Public License version
     18  * 2 along with this work; if not, write to the Free Software Foundation,
     19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     20  *
     21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     22  * or visit www.oracle.com if you need additional information or have any
     90         super(s, undeclaredThrowable);  // Disallow initCause
     91     }
     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 the undeclared exception
    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      * @param  s the {@code ObjectInputStream} from which data is read
    123      * @throws IOException if an I/O error occurs
    124      * @throws ClassNotFoundException if a serialized class cannot be loaded
    125      */
    126     @java.io.Serial
    127     private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException {
    128         ObjectInputStream.GetField fields = s.readFields();
    129         Throwable exception = (Throwable) fields.get("undeclaredThrowable", null);
    130         if (exception != null) {
    131             SharedSecrets.getJavaLangAccess().setCause(this, exception);
    132         }
    133     }
    134
    135     /**
    136      * To maintain compatibility with older implementation, write a serial
    137      * "ex" field with the cause as the value.
    138      *
    139      * @param  out the {@code ObjectOutputStream} to which data is written
    140      * @throws IOException if an I/O error occurs
    141      */
    142     @java.io.Serial
    143     private void writeObject(ObjectOutputStream out) throws IOException {
    144         ObjectOutputStream.PutField fields = out.putFields();
    145         fields.put("undeclaredThrowable", super.getCause());
    146         out.writeFields();
    147     }
    148 }

< prev index next >