17

I tried to catch an Exception but the compiler gives warning: This type test or downcast will always hold

let testFail () =
    try
        printfn "Ready for failing..."
        failwith "Fails"
    with
    | :? System.ArgumentException -> ()
    | :? System.Exception -> ()

The question is: how to I do it without the warning? (I believe there must be a way to do this, otherwise there should be no warning)

Like C#

try
{
    Console.WriteLine("Ready for failing...");
    throw new Exception("Fails");
}
catch (Exception)
{
}
2
  • 1
    Your C# example causes a warning for anyone using ReSharper or FxCop. Why? Commented Jul 7, 2011 at 20:31
  • 1
    @Joel Mueller, thank you for your article. I don't like to catch general exception either, but sometimes unexpected exception may confuse users.
    – LLS
    Commented Jul 8, 2011 at 7:28

2 Answers 2

37

C#:

void testFail()
{
    try
    {
        Console.WriteLine("Ready for failing...");
        throw new Exception("Fails");
    }
    catch (ArgumentException)
    {
    }
    catch
    {
    }
}

F# equivalent:

let testFail () =
    try
        printfn "Ready for failing..."
        failwith "Fails"
    with
    | :? System.ArgumentException -> ()
    | _ -> ()

C#:

void testFail()
{
    try
    {
        Console.WriteLine("Ready for failing...");
        throw new Exception("Fails");
    }
    catch (ArgumentException ex)
    {
    }
    catch (Exception ex)
    {
    }
}

F# equivalent:

let testFail () =
    try
        printfn "Ready for failing..."
        failwith "Fails"
    with
    | :? System.ArgumentException as ex -> ()
    | ex -> ()

C#:

void testFail()
{
    try
    {
        Console.WriteLine("Ready for failing...");
        throw new Exception("Fails");
    }
    catch
    {
    }
}

F# equivalent:

let testFail () =
    try
        printfn "Ready for failing..."
        failwith "Fails"
    with
    | _ -> ()

As Joel noted, you would not want to use catch (Exception) in C# for the same reason you don't use | :? System.Exception -> in F#.

2
  • Thank you. That's very detailed. After reading Joel's comment, I will try to avoid catching general exception. But I guess sometimes it may be necessary to fail gracefully... (At least give error messages in user-friendly way)
    – LLS
    Commented Jul 8, 2011 at 7:32
  • 1
    @LLS : It's not that catching the base Exception type is necessarily a problem, it's that catch(Exception){} and catch{} have identical semantics, so the former is redundant and produces a warning. If you name the exception object, catch(Exception ex){}, there's no warning.
    – ildjarn
    Commented Jul 8, 2011 at 13:26
7
try 
  .. code ..
with
  | _ as e -> 
3
  • I did not know you could alias the wildcard match like that. That's really handy (and not just for exception handling)
    – piers7
    Commented Oct 18, 2016 at 14:17
  • 2
    The _ is really redundant, and actually this code undermines the purpose of the _ variable | _ as e -> (...) does nothing different from | e -> (..) any var without guard clause in a pattern matching statement is a catch all, naming the variable _ simply states that you don't care about the value. Aliasing the _ kind of undermines the _ in the first place.
    – Vidar
    Commented Jan 11, 2017 at 10:27
  • This should be the accepted answer. The lecture in the currently-accepted answer is unnecessary and unhelpful. I just want to turn 2 exceptions' messages into a Result of string error in my hackathon project Commented Nov 6, 2020 at 0:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.