繁体   English   中英

在Visual Studio上调试F#代码时,未处理的异常的位置不正确

[英]Location of unhandled exception is incorrect when debugging F# code on Visual Studio

在调试C#代码时,Visual Studio会显示未处理异常的原始位置。

例如,在调试以下代码时,Visual Studio会在第9行显示未处理的DivideByZeroException。

using System;

namespace ConsoleApplication8
{
    class Program
    {
        static void Func()
        {
            throw new DivideByZeroException(); // line 9
        }

        static void Main(string[] args)
        {
            try { Func(); }
            catch (ArgumentException) { }
        }
    }
}

我想在F#中做同样的事情。 以下是我将上述代码翻译成F#。

open System

let f() = raise (new DivideByZeroException()) // line 3

[<EntryPoint>]
let main argv = 
    try 
        f()
    with :? ArgumentException -> () // line 9
    0

当我在Visual Studio上调试此代码时,它会中断未处理的异常并指向第9行。我希望它指向第3行。

我尝试过Microsoft.FSharp.Core.Operators.reraise但它没有改变结果。

编辑

C#调试屏幕截图

调试屏幕,C#

F#调试屏幕截图

调试屏幕,F#

为C#生成IL

ILSpy for C#

为F#生成IL

ILSpy for F#

问题是当您在调用f()时添加了try / with语句时,抛出的异常会传播到代码的“main”块中。

为了处理函数f()中的异常,如在C#示例中那样,将try / with移动到f(),如下所示:

let f() = 
 try
   raise (System.DivideByZeroException()) // exception is handled here
   with | InnerError(str) -> printfn "Error1 %s" str

[<EntryPoint>]
let main argv = 
    f()
    0

我看到了同样的问题,并提出了一个问题 解决方法是将--generate-filter-blocks添加到项目属性的“Build”页面中的“Other flags”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM