ConfirmAssert[test]
confirms that test is True, otherwise throwing an error to the nearest surrounding Enclose.
ConfirmAssert[test,info]
evaluates info and includes its value in the thrown error if test is not True.
ConfirmAssert[test,info,tag]
uses the specified tag for any thrown errors.
ConfirmAssert
ConfirmAssert[test]
confirms that test is True, otherwise throwing an error to the nearest surrounding Enclose.
ConfirmAssert[test,info]
evaluates info and includes its value in the thrown error if test is not True.
ConfirmAssert[test,info,tag]
uses the specified tag for any thrown errors.
Details
- ConfirmAssert[…] always returns Null.
- ConfirmAssert is dynamically scoped when given a tag. Otherwise, it is lexically scoped. »
- The default value of info is Null.
- Failure objects generated by ConfirmAssert include the following fields:
-
"Information" info from ConfirmAssert[test,info,…] "Test" expr from ConfirmAssert[test,…] - Values of these fields can be extracted using failure["prop"] or directly through Enclose[expr,"prop"].
Examples
open all close allBasic Examples (2)
Confirm that an assertion is true:
Enclose[ConfirmAssert[2 > 1];2]If the assertion fails, stop evaluation and throw an error to the surrounding Enclose:
Enclose[ConfirmAssert[2 > 3];2]Create a function that checks its arguments:
f[x_] := Enclose[ConfirmAssert[Sqrt[x] > 2];Sin[x]]f[5.3]f[3.4]Scope (3)
ConfirmAssert stops evaluation and returns to the matching Enclose when it throws an error:
Enclose[Echo[1];ConfirmAssert[1 == 0];Echo[2]]info is only evaluated when expr cannot be confirmed:
Enclose[ConfirmAssert[1 < 5, Echo["info was evaluated"]]]Enclose[ConfirmAssert[12 < 5, Echo["info was evaluated"]]]Create a function that calls ConfirmAssert with a tag so that it can be caught dynamically:
f[x_] := g[ConfirmAssert[StringQ[x], Null, "fooTag"];StringReverse[x]]h[y_] := Enclose[f[y], Identity, "fooTag"]h["reverseme"]h[42]If no tag is given, ConfirmAssert is lexically scoped:
f2[x_] := g[ConfirmAssert[StringQ[x]];StringReverse[x]]h2[y_] := Enclose[f2[y]]h2[2]Applications (1)
Print a message when a failure is detected:
reverseRoman::nan = "`` is not an integer.";reverseRoman[n_] :=
Enclose[
ConfirmAssert[IntegerQ[n], Message[reverseRoman::nan, n]];
StringReverse[RomanNumeral[n]],
$Failed&]reverseRoman[21]reverseRoman["veni, vidi, vici"]Properties & Relations (2)
ConfirmAssert can be emulated with ConfirmBy or ConfirmMatch:
Enclose[ConfirmAssert[False];"hello"]Enclose[ConfirmBy[False, TrueQ];"hello"]Enclose[ConfirmMatch[False, True];"hello"]ConfirmAssert[f[expr]] is effectively equivalent to ConfirmBy[expr,f] except that it returns Null instead of expr:
Enclose[f[ConfirmAssert[StringQ["hello"]]]]Enclose[f[ConfirmBy["hello", StringQ]]]Possible Issues (3)
ConfirmAssert must be given a tag or be within a lexical Enclose:
ConfirmAssert[2 < 4]Untagged ConfirmAssert works by replacing instances of the symbol ConfirmAssert with a tagged variant:
Enclose[Hold[ConfirmAssert]]Tagged ConfirmAssert requires an Enclose with a matching tag:
Enclose[ConfirmAssert[1 < 0, Null, "foo"], Identity, "bar"]Related Guides
History
Text
Wolfram Research (2020), ConfirmAssert, Wolfram Language function, https://reference.wolfram.com/language/ref/ConfirmAssert.html.
CMS
Wolfram Language. 2020. "ConfirmAssert." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ConfirmAssert.html.
APA
Wolfram Language. (2020). ConfirmAssert. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ConfirmAssert.html
BibTeX
@misc{reference.wolfram_2026_confirmassert, author="Wolfram Research", title="{ConfirmAssert}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/ConfirmAssert.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_confirmassert, organization={Wolfram Research}, title={ConfirmAssert}, year={2020}, url={https://reference.wolfram.com/language/ref/ConfirmAssert.html}, note=[Accessed: 12-June-2026]}