Exception
Details
- Exception constructs an Exception object of the form Exception[{tag1,tag2,…},data], where {tag1,…} represents exception type(s), and data is an association containing details.
- Possible forms of spec include:
-
tag a string or symbol, representing exception tag {tag1,tag2,…} a list of several exception tags Exception[…] a valid Exception object <|…|> an association, containing full exception data - If spec is a tag or a list of tags, each tag is added to exception's resulting tag list, together with all its parent tags. In the context of exception handling, this process is referred to as tag expansion.
- The payload may be any Wolfram Language expression, with an association being the most common case.
- If spec is an association, it must contain at least one of the following keys to be valid:
-
"ExceptionTagList" fully expanded list of exception tags "ExceptionTag" main exception tag - If payload is an association, the data it contains is added to the constructed exception's data.
- If payload is an arbitrary expression, it is added to the constructed exception's data as "ExceptionPayload"payload.
- Exception[…]["prop"] extracts the a property from an Exception object. Exception[…][{key1,key2,…}] extracts a list of values.
- Exception[…]["Properties"] returns a list of available properties.
Examples
open all close allBasic Examples (2)
This creates an Exception object with a string tag and no additional information:
Exception["DataConsistencyError"]This creates an Exception object with a string tag and some additional information:
Exception["DataConsistencyError", <|"Data" -> 42|>]This creates an Exception object with a symbolic tag:
Exception[ArgumentError, <|"SomeKey" -> "SomeValue"|>]This creates an Exception object with a string tag and a general expression as a payload:
Exception["InvalidExpression", a + b]One can inspect the InputForm of the resulting Exception object:
Exception["InvalidExpression", a + b]//InputFormScope (8)
Using Registered Exception Types (1)
This registers the exception tag as a child type of another exception type:
RegisterExceptionType[ArgumentTypeError, ArgumentError]This constructs an Exception object with the registered tag:
Exception[ArgumentTypeError, <|"InvalidArgumentValue" -> Pi, "ExpectedArgumentType" -> Integer|>]This creates a more exotic Exception object with two exception types:
Exception[{"UnknownError", ArgumentTypeError}]This unregisters the ArgumentTypeError exception type:
Unprotect[ArgumentTypeError]
DeleteObject[ArgumentTypeError]Properties (1)
This registers the exception tag as a child type of another exception tag:
RegisterExceptionType[ArgumentTypeError, ArgumentError]This constructs an Exception object with the registered tag:
exc = Exception[ArgumentTypeError, <|"InvalidArgumentValue" -> Pi, "ExpectedArgumentType" -> Integer|>]The following exception properties are available:
exc["Properties"]This extracts the main tag associated with the exception:
exc["ExceptionTag"]This extracts the fully expanded list of exception tags:
exc["ExceptionTagList"]This extracts the data provided at exception construction:
exc[{"InvalidArgumentValue", "ExpectedArgumentType"}]This extracts full association with the exception's data:
exc["ExceptionData"]One can use this association to reconstruct the exception:
Exception[exc["ExceptionData"]]This unregisters the ArgumentTypeError exception type:
Unprotect[ArgumentTypeError]
DeleteObject[ArgumentTypeError]Incorporating Message Information (1)
This creates an exception with certain specific message information:
Exception["ArgumentError", <|"MessageTemplate" -> "Argument number `` has the wrong type", "MessageParameters" -> {2}|>]One can create an Exception object using the existing symbol's message:
Exception["DivisionByZero", <|"MessageTemplate" :> Power::infy, "MessageParameters" -> {HoldForm[1 / 0]}|>]Conversion to Failure Objects (3)
This creates an Exception:
exc = Exception[ArgumentErrorException, <|"Data" -> 42|>]This converts it to a Failure. By default, the string tag for the resulting Failure object is obtained by direct stringification of the main tag of the exception being converted:
exc["ExceptionFailure"]The conversion is lossless, and one can reconstruct the original exception:
Exception[exc["ExceptionFailure"]]This creates an Exception, specifying which tag must be used for conversion of it to a Failure object:
exc = Exception[ArgumentErrorException, <|"Data" -> 42, "ExceptionFailureTag" -> "ArgumentError"|>]This converts it to a Failure, using the specified string tag for the resulting Failure object:
exc["ExceptionFailure"]This creates a Failure object:
Failure["GenericError", <|"ExceptionTagList" -> {GenericException}|>]This Failure object can be directly converted to an Exception, because it contains the information about exception tags (the "ExceptionTagList" property):
Exception[Failure["GenericError", <|"ExceptionTagList" -> {GenericException}|>]]Low-Level Exception Construction from Exception Data (1)
This constructs an Exception object directly from the association containing its data:
Exception[<|"ExceptionTagList" -> {ArgumentError}, "InvalidArgumentValue" -> Pi, "ExpectedArgumentType" -> Integer|>]Note that at least one of "ExceptionTag" or "ExceptionTagList" keys must be present for this construction mode.
Constructing New Exception Object from Existing Exception (1)
The Exception constructor is idempotent:
Exception[Exception["DataConsistencyError", <|"Data" -> 42|>]]In the case of additional data passed as payload, that data will be appended:
Exception[Exception["DataConsistencyError", <|"Data" -> 42|>], <|"IntermediateFunction" -> func|>]If the additional data contains the keys that collide with keys already present in the exception's data, that data will be overwritten:
Exception[Exception["DataConsistencyError", <|"Data" -> 42|>], <|"IntermediateFunction" -> func, "Data" -> 100|>]However, the additional data should not contain any of the system keys, such as "ExceptionTagList" or "ExceptionTag":
Exception[Exception["DataConsistencyError", <|"Data" -> 42|>], <|"IntermediateFunction" -> func, "Data" -> 100, "ExceptionTagList" -> {"NewTag"}|>]Tech Notes
Related Guides
History
Text
Wolfram Research (2026), Exception, Wolfram Language function, https://reference.wolfram.com/language/ref/Exception.html.
CMS
Wolfram Language. 2026. "Exception." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/Exception.html.
APA
Wolfram Language. (2026). Exception. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Exception.html
BibTeX
@misc{reference.wolfram_2026_exception, author="Wolfram Research", title="{Exception}", year="2026", howpublished="\url{https://reference.wolfram.com/language/ref/Exception.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_exception, organization={Wolfram Research}, title={Exception}, year={2026}, url={https://reference.wolfram.com/language/ref/Exception.html}, note=[Accessed: 12-June-2026]}