1 /*
2  * Copyright (c) 2000, 2011, 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
23  * questions.
24  */
25 
26 package java.lang;
27 
28 /**
29  * Thrown to indicate that an assertion has failed.
30  *
31  * <p>The seven one-argument public constructors provided by this
32  * class ensure that the assertion error returned by the invocation:
33  * <pre>
34  *     new AssertionError(<i>expression</i>)
35  * </pre>
36  * has as its detail message the <i>string conversion</i> of
37  * <i>expression</i> (as defined in section 15.18.1.1 of
38  * <cite>The Java&trade; Language Specification</cite>),
39  * regardless of the type of <i>expression</i>.
40  *
41  * @since   1.4
42  */
43 public class AssertionError extends Error {
44     private static final long serialVersionUID = -5013299493970297370L;
45 
46     /**
47      * Constructs an AssertionError with no detail message.
48      */
AssertionError()49     public AssertionError() {
50     }
51 
52     /**
53      * This internal constructor does no processing on its string argument,
54      * even if it is a null reference.  The public constructors will
55      * never call this constructor with a null argument.
56      */
AssertionError(String detailMessage)57     private AssertionError(String detailMessage) {
58         super(detailMessage);
59     }
60 
61     /**
62      * Constructs an AssertionError with its detail message derived
63      * from the specified object, which is converted to a string as
64      * defined in section 15.18.1.1 of
65      * <cite>The Java&trade; Language Specification</cite>.
66      *<p>
67      * If the specified object is an instance of {@code Throwable}, it
68      * becomes the <i>cause</i> of the newly constructed assertion error.
69      *
70      * @param detailMessage value to be used in constructing detail message
71      * @see   Throwable#getCause()
72      */
AssertionError(Object detailMessage)73     public AssertionError(Object detailMessage) {
74         this(String.valueOf(detailMessage));
75         if (detailMessage instanceof Throwable)
76             initCause((Throwable) detailMessage);
77     }
78 
79     /**
80      * Constructs an AssertionError with its detail message derived
81      * from the specified <code>boolean</code>, which is converted to
82      * a string as defined in section 15.18.1.1 of
83      * <cite>The Java&trade; Language Specification</cite>.
84      *
85      * @param detailMessage value to be used in constructing detail message
86      */
AssertionError(boolean detailMessage)87     public AssertionError(boolean detailMessage) {
88         this(String.valueOf(detailMessage));
89     }
90 
91     /**
92      * Constructs an AssertionError with its detail message derived
93      * from the specified <code>char</code>, which is converted to a
94      * string as defined in section 15.18.1.1 of
95      * <cite>The Java&trade; Language Specification</cite>.
96      *
97      * @param detailMessage value to be used in constructing detail message
98      */
AssertionError(char detailMessage)99     public AssertionError(char detailMessage) {
100         this(String.valueOf(detailMessage));
101     }
102 
103     /**
104      * Constructs an AssertionError with its detail message derived
105      * from the specified <code>int</code>, which is converted to a
106      * string as defined in section 15.18.1.1 of
107      * <cite>The Java&trade; Language Specification</cite>.
108      *
109      * @param detailMessage value to be used in constructing detail message
110      */
AssertionError(int detailMessage)111     public AssertionError(int detailMessage) {
112         this(String.valueOf(detailMessage));
113     }
114 
115     /**
116      * Constructs an AssertionError with its detail message derived
117      * from the specified <code>long</code>, which is converted to a
118      * string as defined in section 15.18.1.1 of
119      * <cite>The Java&trade; Language Specification</cite>.
120      *
121      * @param detailMessage value to be used in constructing detail message
122      */
AssertionError(long detailMessage)123     public AssertionError(long detailMessage) {
124         this(String.valueOf(detailMessage));
125     }
126 
127     /**
128      * Constructs an AssertionError with its detail message derived
129      * from the specified <code>float</code>, which is converted to a
130      * string as defined in section 15.18.1.1 of
131      * <cite>The Java&trade; Language Specification</cite>.
132      *
133      * @param detailMessage value to be used in constructing detail message
134      */
AssertionError(float detailMessage)135     public AssertionError(float detailMessage) {
136         this(String.valueOf(detailMessage));
137     }
138 
139     /**
140      * Constructs an AssertionError with its detail message derived
141      * from the specified <code>double</code>, which is converted to a
142      * string as defined in section 15.18.1.1 of
143      * <cite>The Java&trade; Language Specification</cite>.
144      *
145      * @param detailMessage value to be used in constructing detail message
146      */
AssertionError(double detailMessage)147     public AssertionError(double detailMessage) {
148         this(String.valueOf(detailMessage));
149     }
150 
151     /**
152      * Constructs a new {@code AssertionError} with the specified
153      * detail message and cause.
154      *
155      * <p>Note that the detail message associated with
156      * {@code cause} is <i>not</i> automatically incorporated in
157      * this error's detail message.
158      *
159      * @param  message the detail message, may be {@code null}
160      * @param  cause the cause, may be {@code null}
161      *
162      * @since 1.7
163      */
AssertionError(String message, Throwable cause)164     public AssertionError(String message, Throwable cause) {
165         super(message, cause);
166     }
167 }
168