【问题标题】:f# handling exceptions for multiple exception typesf# 处理多种异常类型的异常
【发布时间】:2015-05-15 18:12:34
【问题描述】:

我正在尝试根据异常类型(如 c#)捕获异常,但是当我执行以下操作时出现编译器错误 This type test or downcast will always 在线| :? System.Exception as genericException -> F# 中不能有多个“catch”块吗?

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

【问题讨论】:

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


【解决方案1】:

因为:? 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#?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    相关资源
    最近更新 更多