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 allclose allBasic Examples (2)
Scope (6)
Process an error before returning:
Set f to "Expression" to propagate errors without wrapping in Failure:
The innermost Enclose has precedence:
Use a tag to pass an error to the outer Enclose:
Provide a constant function for f to return a fixed value in the event of any error:
Applications (3)
Safely call a function that could fail, propagating any errors unchanged:
Propagate missing values while calculating GDP per capita for every country:
Find all polyhedra that have edges between every pair of vertices, ignoring polyhedra where no BoundaryMeshRegion is available:
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