CatchExceptions[expr,spec]
catches and transforms some or all exceptions possibly thrown by expr, according to spec.
CatchExceptions[spec]
represents an operator form of CatchExceptions.
CatchExceptions
CatchExceptions[expr,spec]
catches and transforms some or all exceptions possibly thrown by expr, according to spec.
CatchExceptions[spec]
represents an operator form of CatchExceptions.
Details
- CatchExceptions catches exceptions that match spec and lets other exceptions propagate further.
- The specification spec can take one of the following forms:
-
tagspec exception tag specification tagspechandler exception tag and handler specification {tagspec1handler1,…} multiple handlers for different tag specifications - The different forms of tagspec have the following exception-matching semantics:
-
All all exceptions tag all exceptions of type tag or its subtypes {tag1,tag2,…} all exceptions of any of the types tagn or their subtypes - CatchExceptions[expr,tagspechandler] can have the following return values:
-
expr no exception was thrown in expr, which is then returned handler[exceptionData] exception caught, handler applied to its data association - The handler can be any function. It is passed an association (exception data) as a single argument.
- When handler is not specified, the default handler is used. It automatically converts exception data to a Failure object.
- When spec consists of several clauses, the first one that matches the thrown Exception is used.
- Any exceptions thrown by the code of exception handlers in spec are not handled by the current invocation of CatchExceptions. Those will require a separate CatchExceptions call wrapped around, to be handled.
Examples
open all close allBasic Examples (1)
This catches all thrown exceptions and uses the default exception handler to handle them:
CatchExceptions[ThrowException["Error", 42], All]This does exactly the same, but using the operator form of CatchExceptions:
CatchExceptions[All][ThrowException["Error", 42]]In the case of normal execution (no exceptions thrown), the value of the passed expression is returned:
CatchExceptions[1 + 2 + 3, All]One can specify the handler function to handle caught exceptions:
CatchExceptions[ThrowException["Error", 42], All -> handler]Scope (2)
This will only catch exceptions of type "ArgumentError":
CatchExceptions[ThrowException["ArgumentError", 100], "ArgumentError"]But not exceptions of other types:
CatchExceptions[ThrowException["Error", 42], "ArgumentError"]This registers the subtype of the type "ArgumentError", using RegisterExceptionType:
RegisterExceptionType[ArgumentTypeError, "ArgumentError"]One can use the parent exception type to also catch exceptions of its subtypes:
CatchExceptions["ArgumentError" -> argErrorHandler][ThrowException[ArgumentTypeError, 42]]One can also use the multiple clause syntax, with the parent exception type (or All) as a catchall clause:
CatchExceptions[{
ArgumentTypeError -> argTypeErrorHandler, "ArgumentError" -> argErrorHandler
}][ThrowException[ArgumentTypeError, 42]]This will conditionally rethrow the caught exception, based on the data of the caught exception:
CatchExceptions[{
ArgumentTypeError -> Function[If[#ExceptionPayload > 10, ThrowException @ #, #ExceptionPayload]],
"ArgumentError" -> argErrorHandler
}][ThrowException[ArgumentTypeError, 42]]One would need an additional call to CatchExceptions to handle the rethrown exception.
Tech Notes
Related Guides
History
Text
Wolfram Research (2026), CatchExceptions, Wolfram Language function, https://reference.wolfram.com/language/ref/CatchExceptions.html.
CMS
Wolfram Language. 2026. "CatchExceptions." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/CatchExceptions.html.
APA
Wolfram Language. (2026). CatchExceptions. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/CatchExceptions.html
BibTeX
@misc{reference.wolfram_2026_catchexceptions, author="Wolfram Research", title="{CatchExceptions}", year="2026", howpublished="\url{https://reference.wolfram.com/language/ref/CatchExceptions.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_catchexceptions, organization={Wolfram Research}, title={CatchExceptions}, year={2026}, url={https://reference.wolfram.com/language/ref/CatchExceptions.html}, note=[Accessed: 12-June-2026]}