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