Enclose[expr]
attempts to evaluate expr and return the result, but stops if it catches an error and returns a failure object.
Enclose[expr,f]
applies f to any failure object generated.
Enclose[expr,"prop"]
gives the property prop of any failure object generated.
Enclose[expr,handler,form]
only catches errors with explicitly specified tags matching form.
Enclose
Enclose[expr]
attempts to evaluate expr and return the result, but stops if it catches an error and returns a failure object.
Enclose[expr,f]
applies f to any failure object generated.
Enclose[expr,"prop"]
gives the property prop of any failure object generated.
Enclose[expr,handler,form]
only catches errors with explicitly specified tags matching form.
Details
- Enclose[expr,…] returns the value of expr if no errors were caught during its evaluation.
- Enclose will not catch errors that have already been caught by an inner Enclose.
- Enclose is lexically scoped when no tag is given, and dynamically scoped otherwise. Enclose[expr] therefore handles only instances of Confirm, etc. that lexically appear in the expression expr. It does not handle instances of Confirm, etc. that are encountered only when expr is evaluated.
- tag can be any expression including a pattern.
- Enclose catches errors thrown by Confirm, ConfirmBy, ConfirmMatch, ConfirmQuiet and ConfirmAssert.
- Enclose effectively uses Catch with a special tag.
- The default value of f in Enclose[expr,f,…] is Identity.
- In Enclose[expr,"prop"], the properties that can be requested depend on the error thrown. »
Examples
open all close allBasic Examples (2)
Evaluate an expression, exiting as soon as any errors are thrown:
Enclose[2 + 2]Enclose[2 + 2;Confirm[$Failed];4 + 4]addtwo[x_] := Enclose[ConfirmBy[x, NumberQ] + 2]addtwo[4]addtwo[x]Scope (6)
Process an error before returning:
Enclose[2 + 2;Confirm[$Failed];4 + 4, f]Set f to "Expression" to propagate errors without wrapping in Failure:
Enclose[f[Confirm[Missing["NA"]]], "Expression"]The innermost Enclose has precedence:
Enclose[a;
Enclose[b;
Confirm[$Failed];
c];
d]Use a tag to pass an error to the outer Enclose:
Enclose[a;Enclose[b;Confirm[$Failed, None, "tag"];c];d, Identity, "tag"]Provide a constant function for f to return a fixed value in the event of any error:
recip[x_] := Enclose[1 / ConfirmBy[x, GreaterThan[0] @* Abs], Missing[]&]recip[2]recip[0]Propagate errors up multiple levels:
addtwo[x_] := Enclose[Confirm[x] + 2]
framedAddtwo[x_] := Enclose[Framed[Confirm[addtwo[x]]]]framedAddtwo[5]framedAddtwo[$Failed]Perform multiple confirmations by nesting confirms:
Enclose[f[ConfirmBy[ConfirmMatch[x, _Integer], EulerPhi[#] > 3&]]]Applications (3)
Safely call a function that could fail, propagating any errors unchanged:
cityPopulation[s_] := Enclose[Confirm[Interpreter["City"][s]]["Population"], "Expression"]cityPopulation["nyc"]cityPopulation["null island"]Propagate missing values while calculating GDP per capita for every country:
Short[Enclose[Confirm[#[[1]]] / Confirm[#[[2]]], Missing[]&]& /@ EntityValue["Country", {"GDP", "Population"}]]Find all polyhedra that have edges between every pair of vertices, ignoring polyhedra where no BoundaryMeshRegion is available:
Keys[Select[EntityValue["Polyhedron", "BoundaryMeshRegion", "EntityAssociation"], Enclose[CompleteGraphQ[MeshConnectivityGraph[Confirm[#], 2]], False&]&]]Possible Issues (3)
Catch[…,_] can intercept errors:
Enclose[Catch[Confirm[$Failed], _];d]Enclose does not catch expressions thrown by Throw:
Enclose[Throw[a];b]If no tag is provided, Enclose lexically replaces symbols like Confirm with lexically tagged variants:
Enclose[Hold[Confirm]]Related Guides
History
Text
Wolfram Research (2020), Enclose, Wolfram Language function, https://reference.wolfram.com/language/ref/Enclose.html.
CMS
Wolfram Language. 2020. "Enclose." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/Enclose.html.
APA
Wolfram Language. (2020). Enclose. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Enclose.html
BibTeX
@misc{reference.wolfram_2026_enclose, author="Wolfram Research", title="{Enclose}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/Enclose.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_enclose, organization={Wolfram Research}, title={Enclose}, year={2020}, url={https://reference.wolfram.com/language/ref/Enclose.html}, note=[Accessed: 12-June-2026]}