如何捕获任何异常(System.Exception)在F#中没有警告? [英] How to catch any exception (System.Exception) without a warning in F#?

查看:92
本文介绍了如何捕获任何异常(System.Exception)在F#中没有警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图捕获一个异常,但编译器发出警告:这种类型的测试或downcast将始终保持

  let testFail )= 
try
printfn准备失败...
failwithFails
with
| :? System.ArgumentException - > ()
| :? System.Exception - > ()

问题是:如何做到没有警告? (我相信必须有办法这样做,否则应该没有警告)



喜欢C#

  try 
{
Console.WriteLine(准备失败...);
抛出新异常(失败);
}
catch(异常)
{
}


解决方案

C#:

  void testFail b $ b {
try
{
Console.WriteLine(准备失败...);
抛出新异常(失败);
}
catch(ArgumentException)
{
}
catch
{
}
}
$ / pre

F#等效:

 $ $ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ $ | | | | | | | | | | | | | | | | | | | | | | | |


:? System.ArgumentException - > ()
| _ - > ()






C#:

  void testFail()
{
try
{
控制台。 WriteLine(准备失败...);
抛出新异常(失败);
}
catch(ArgumentException ex)
{
}
catch(Exception ex)
{
}
}

F#等价:

尝试
printfn准备失败...
failwithFails
with
| :? System.ArgumentException as ex - > ()
| ex - > ()






C#:

  void testFail()
{
try
{
控制台。 WriteLine(准备失败...);
抛出新异常(失败);
}
catch
{
}
}

F#等价:

  let testFail()= 
try
printfn准备失败...
failwithFails
with
| _ - > ()






正如乔尔所说,你不会想在C#中使用 catch(Exception),因为您不使用 | :? System.Exception - > 在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)
{
}

解决方案

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

这篇关于如何捕获任何异常(System.Exception)在F#中没有警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆