我有一个看起来很像的代码:
ignore
(f ())
handle
AssertionError msg => (print ("assertion error: " ^ msg ^ "\n"); ())
| _ (* other exceptions *) => (print ("exception raised\n"); ())
但我需要打印通用异常消息(使用 exnMessage
?)。
如何捕获 _
异常以获取并打印其消息?
最佳答案
使用名称而不是 _
来匹配异常并使用 exnMessage
:
- (hd ([]: string list)) handle e => exnMessage e;
val it = "Empty" : string
关于exception - 使用 SML 处理和打印异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52704243/