Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch any exception (System.Exception) without a warning in F#?

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)
{
}
like image 338
LLS Avatar asked Jul 07 '11 20:07

LLS


People also ask

How do you handle system exception?

If you catch System. Exception , rethrow it using the throw keyword at the end of the catch block. If a catch block defines an exception variable, you can use it to obtain more information about the type of exception that occurred. Exceptions can be explicitly generated by a program by using the throw keyword.

How do you catch an unknown exception in Python?

Another way to catch all Python exceptions when it occurs during runtime is to use the raise keyword. It is a manual process wherein you can optionally pass values to the exception to clarify the reason why it was raised. if x <= 0: raise ValueError(“It is not a positive number!”)

How do you catch all exceptions?

Exception handling is used to handle the exceptions. We can use try catch block to protect the code. Catch block is used to catch all types of exception. The keyword “catch” is used to catch exceptions.

What can be used in the except block to catch all exceptions in Python?

Catching Specific Exceptions in Python A try clause can have any number of except clauses to handle different exceptions, however, only one will be executed in case an exception occurs. We can use a tuple of values to specify multiple exceptions in an except clause.


1 Answers

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

like image 118
ildjarn Avatar answered Sep 29 '22 04:09

ildjarn