Path: blob/master/src/java.base/share/classes/java/lang/AssertionError.java
41152 views
/*1* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.lang;2627/**28* Thrown to indicate that an assertion has failed.29*30* <p>The seven one-argument public constructors provided by this31* class ensure that the assertion error returned by the invocation:32* <pre>33* new AssertionError(<i>expression</i>)34* </pre>35* has as its detail message the <i>string conversion</i> of36* <i>expression</i> (as defined in section {@jls 5.1.11} of37* <cite>The Java Language Specification</cite>),38* regardless of the type of <i>expression</i>.39*40* @since 1.441*/42public class AssertionError extends Error {43@java.io.Serial44private static final long serialVersionUID = -5013299493970297370L;4546/**47* Constructs an AssertionError with no detail message.48*/49public AssertionError() {50}5152/**53* This internal constructor does no processing on its string argument,54* even if it is a null reference. The public constructors will55* never call this constructor with a null argument.56*/57private AssertionError(String detailMessage) {58super(detailMessage);59}6061/**62* Constructs an AssertionError with its detail message derived63* from the specified object, which is converted to a string as64* defined in section {@jls 5.1.11} of65* <cite>The Java Language Specification</cite>.66*<p>67* If the specified object is an instance of {@code Throwable}, it68* becomes the <i>cause</i> of the newly constructed assertion error.69*70* @param detailMessage value to be used in constructing detail message71* @see Throwable#getCause()72*/73public AssertionError(Object detailMessage) {74this(String.valueOf(detailMessage));75if (detailMessage instanceof Throwable)76initCause((Throwable) detailMessage);77}7879/**80* Constructs an AssertionError with its detail message derived81* from the specified {@code boolean}, which is converted to82* a string as defined in section {@jls 5.1.11} of83* <cite>The Java Language Specification</cite>.84*85* @param detailMessage value to be used in constructing detail message86*/87public AssertionError(boolean detailMessage) {88this(String.valueOf(detailMessage));89}9091/**92* Constructs an AssertionError with its detail message derived93* from the specified {@code char}, which is converted to a94* string as defined in section {@jls 5.1.11} of95* <cite>The Java Language Specification</cite>.96*97* @param detailMessage value to be used in constructing detail message98*/99public AssertionError(char detailMessage) {100this(String.valueOf(detailMessage));101}102103/**104* Constructs an AssertionError with its detail message derived105* from the specified {@code int}, which is converted to a106* string as defined in section {@jls 5.1.11} of107* <cite>The Java Language Specification</cite>.108*109* @param detailMessage value to be used in constructing detail message110*/111public AssertionError(int detailMessage) {112this(String.valueOf(detailMessage));113}114115/**116* Constructs an AssertionError with its detail message derived117* from the specified {@code long}, which is converted to a118* string as defined in section {@jls 5.1.11} of119* <cite>The Java Language Specification</cite>.120*121* @param detailMessage value to be used in constructing detail message122*/123public AssertionError(long detailMessage) {124this(String.valueOf(detailMessage));125}126127/**128* Constructs an AssertionError with its detail message derived129* from the specified {@code float}, which is converted to a130* string as defined in section {@jls 5.1.11} of131* <cite>The Java Language Specification</cite>.132*133* @param detailMessage value to be used in constructing detail message134*/135public AssertionError(float detailMessage) {136this(String.valueOf(detailMessage));137}138139/**140* Constructs an AssertionError with its detail message derived141* from the specified {@code double}, which is converted to a142* string as defined in section {@jls 5.1.11} of143* <cite>The Java Language Specification</cite>.144*145* @param detailMessage value to be used in constructing detail message146*/147public AssertionError(double detailMessage) {148this(String.valueOf(detailMessage));149}150151/**152* Constructs a new {@code AssertionError} with the specified153* detail message and cause.154*155* <p>Note that the detail message associated with156* {@code cause} is <i>not</i> automatically incorporated in157* 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.7163*/164public AssertionError(String message, Throwable cause) {165super(message, cause);166}167}168169170