f#处理多个异常类型的异常

时间:2015-05-15 10:08:13

标签: f# c#-to-f#

我试图根据c#中的异常类型捕获异常,但是当我执行以下操作时遇到编译器错误 第This type test or downcast will always行的| :? System.Exception as genericException -> 你能不能拥有多个" catch" F#中的块?

try
    ......
with
| :? System.AggregateException as aggregateException ->
   for innerException in aggregateException.InnerExceptions do
                log message
| :? System.Exception as genericException ->
           log message

1 个答案:

答案 0 :(得分:2)

这是因为:? System.Exception as是多余的,代码可以简化为以下内容:

try
    // ...
with
| :? System.AggregateException as aggregateException ->
    for innerException in aggregateException.InnerExceptions do
        log message
| genericException -> log message

有关更多示例,请参阅此答案:How to catch any exception (System.Exception) without a warning in F#?