4
votes

I am trying to catch exceptions based on exception type like in c#, but I am getting compiler error when I do the following This type test or downcast will always at line | :? System.Exception as genericException -> Can you not have multiple "catch" blocks in F#?

try
    ......
with
| :? System.AggregateException as aggregateException ->
   for innerException in aggregateException.InnerExceptions do
                log message
| :? System.Exception as genericException ->
           log message
1
I don't think you need the explicit type test, the last clause can just be | genericException -> log message - Lee

1 Answers

3
votes

It's because :? System.Exception as is redundant, and the code can be reduced to the following:

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

See this answer for further examples: How to catch any exception (System.Exception) without a warning in F#?