Exception
更多信息
- Exception 构造 Exception 对象,其形式为 Exception[{tag1,tag2,…},data],其中 {tag1,…} 表示异常类型,而 data 是包含详细信息的关联.
- spec 的可能形式包括:
-
tag 表示异常标签的字符串或符号 {tag1,tag2,…} 多个异常标签的列表 Exception[…] 有效的 Exception 对象 <|…|> 包含完整异常数据的关联 - 如果 spec 是一个标签或标签列表,则每个标签及其所有父标签都会被添加到异常的最终标签列表中. 在异常处理的上下文中,这个过程称为标签扩展.
- payload 可以是任何 Wolfram 语言表达式,其中关联是最常见的情况.
- 如果 spec 是关联,则它必须包含以下至少一个键才能有效:
-
"ExceptionTagList" 异常标签的完整列表 "ExceptionTag" 主异常标签 - 如果 payload 是关联,则它包含的数据将添加到所构造异常的数据中.
- 如果 payload 是任意表达式,它将作为 "ExceptionPayload"payload 被添加到所构造异常的数据中.
- Exception[…]["prop"] 从 Exception 对象中提取属性. Exception[…][{key1,key2,…}] 提取值的列表.
- Exception[…]["Properties"] 返回可用属性的列表.
范例
打开所有单元 关闭所有单元基本范例 (2)
这将创建包含字符串标签且不包含任何其他信息的 Exception 对象:
Exception["DataConsistencyError"]这将创建包含字符串标签和一些附加信息的 Exception 对象:
Exception["DataConsistencyError", <|"Data" -> 42|>]这将创建带有符号标记的 Exception 对象:
Exception[ArgumentError, <|"SomeKey" -> "SomeValue"|>]这将创建 Exception 对象,其中包含一个字符串标签和一个通用表达式作为有效负载:
Exception["InvalidExpression", a + b]可以检查生成的 Exception 对象的 InputForm:
Exception["InvalidExpression", a + b]//InputForm范围 (8)
使用注册异常类型 (1)
RegisterExceptionType[ArgumentTypeError, ArgumentError]这将创建带有已注册标签的 Exception 对象:
Exception[ArgumentTypeError, <|"InvalidArgumentValue" -> Pi, "ExpectedArgumentType" -> Integer|>]这将创建更特殊的 Exception 对象,其中包含两种异常类型:
Exception[{"UnknownError", ArgumentTypeError}]这将取消注册 ArgumentTypeError 异常类型:
Unprotect[ArgumentTypeError]
DeleteObject[ArgumentTypeError]属性 (1)
RegisterExceptionType[ArgumentTypeError, ArgumentError]这将创建带有已注册标签的 Exception 对象:
exc = Exception[ArgumentTypeError, <|"InvalidArgumentValue" -> Pi, "ExpectedArgumentType" -> Integer|>]exc["Properties"]exc["ExceptionTag"]exc["ExceptionTagList"]exc[{"InvalidArgumentValue", "ExpectedArgumentType"}]exc["ExceptionData"]Exception[exc["ExceptionData"]]这将取消注册 ArgumentTypeError 异常类型:
Unprotect[ArgumentTypeError]
DeleteObject[ArgumentTypeError]整合消息信息 (1)
Exception["ArgumentError", <|"MessageTemplate" -> "Argument number `` has the wrong type", "MessageParameters" -> {2}|>]可以使用现有符号的消息创建 Exception 对象:
Exception["DivisionByZero", <|"MessageTemplate" :> Power::infy, "MessageParameters" -> {HoldForm[1 / 0]}|>]转换为失败对象 (3)
这将创建一个 Exception:
exc = Exception[ArgumentErrorException, <|"Data" -> 42|>]这会将其转换为 Failure 对象. 默认情况下,生成的 Failure 对象的字符串标签是通过直接字符串化被转换异常的主标签获得的:
exc["ExceptionFailure"]Exception[exc["ExceptionFailure"]]这将创建一个 Exception,并指定必须使用哪个标签才能将其转换为 Failure 对象:
exc = Exception[ArgumentErrorException, <|"Data" -> 42, "ExceptionFailureTag" -> "ArgumentError"|>]这将它转换为 Failure 对象,并使用指定的字符串标签作为生成的 Failure 对象的标签:
exc["ExceptionFailure"]这将创建一个 Failure 对象:
Failure["GenericError", <|"ExceptionTagList" -> {GenericException}|>]这个 Failure 对象可以直接转换为 Exception,因为它包含了有关异常标签的信息("ExceptionTagList" 属性):
Exception[Failure["GenericError", <|"ExceptionTagList" -> {GenericException}|>]]从异常数据构建底层异常 (1)
这将直接从包含其数据的关联中构造 Exception 对象:
Exception[<|"ExceptionTagList" -> {ArgumentError}, "InvalidArgumentValue" -> Pi, "ExpectedArgumentType" -> Integer|>]请注意,对于此构造模式,必须至少提供 "ExceptionTag" 或 "ExceptionTagList" 键中的一个.
从现有异常构造新的异常对象 (1)
Exception 构造函数是幂等的:
Exception[Exception["DataConsistencyError", <|"Data" -> 42|>]]Exception[Exception["DataConsistencyError", <|"Data" -> 42|>], <|"IntermediateFunction" -> func|>]如果附加数据包含与异常数据中已存在的键冲突的键,则该数据将被覆盖:
Exception[Exception["DataConsistencyError", <|"Data" -> 42|>], <|"IntermediateFunction" -> func, "Data" -> 100|>]然而,附加数据不应包含任何系统键,例如 "ExceptionTagList" 或 "ExceptionTag":
Exception[Exception["DataConsistencyError", <|"Data" -> 42|>], <|"IntermediateFunction" -> func, "Data" -> 100, "ExceptionTagList" -> {"NewTag"}|>]技术笔记
-
▪
- 处理异常
文本
Wolfram Research (2026),Exception,Wolfram 语言函数,https://reference.wolfram.com/language/ref/Exception.html.
CMS
Wolfram 语言. 2026. "Exception." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/Exception.html.
APA
Wolfram 语言. (2026). Exception. Wolfram 语言与系统参考资料中心. 追溯自 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: 07-September-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: 07-September-2026]}