Noel
Noel

Reputation: 5399

f# handling exceptions for multiple exception types

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

Upvotes: 4

Views: 803

Answers (1)

ildjarn
ildjarn

Reputation: 62995

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#?

Upvotes: 3

Related Questions