调试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#调试屏幕捕获
F#调试屏幕截图
为C#生成IL
为F#生成IL
答案 0 :(得分:3)
问题是当你在调用f()时添加了try / with语句时,抛出的异常会传播到代码的“main”块中。
为了像在C#示例中那样处理函数f()中的异常,请将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
答案 1 :(得分:0)
我看到同样的问题和filed an issue。解决方法是将--generate-filter-blocks
添加到项目属性的“构建”页面中的“其他标志”。