c# - 为什么 catch block 永远不会在设备上执行?

标签 c# xamarin.ios task-parallel-library xamarin

考虑这段代码:

Task.Factory.StartNew (() => {
    try {
        Console.WriteLine ("Throwing");
        throw new Exception ();
    } catch {
        Console.WriteLine ("Gotcha!");
    }
});

在 iOS 模拟器上,它按预期打印 ThrowingGotcha!
但是,针对设备进行编译时,它仅打印 Throwing 而不会打印 Gotcha!

为什么会发生这种情况?
我使用的是 MonoTouch 6.0.8。
我也可以在 6.0.6 上重现它。

我贴了一个sample project here, you can try to run it on the device .
(别介意名字——当我发现这个时,我正在调查另一起事故)。

最佳答案

我刚刚发现只有启用 LLVM 时才会发生这种情况。
也许这是一个错误,所以我会将其报告给 Xamarin。

有趣的是,添加

BeginInvokeOnMainThread (() => {
    new UIAlertView ("Gotta catch 'em all!", "", null, "OK", null).Show ();
});

Console.WriteLine 调用之后,我看到了警报和日志条目。

这是我的sample project .

关于c# - 为什么 catch block 永远不会在设备上执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13998447/

相关文章:

c# - 具有继承接口(interface)的抽象类 - System.Enum 实现 IConvertible 但 MethodInfo.GetMethods() 不列出那些

C# DataGridView (CheckBox) 单元格点击多次回调

c# - 当我尝试从 Xamarin Samples 运行 Social Framework Demo App 时出现错误

c# - 此代码应返回 Task 还是 Task<object>?

c# - 后台运行方法和UI Thread WPF

c# - 返回带有 XML 数据的 HttpResponseMessage

c# - 字典查找抛出 "Index was outside the bounds of the array"

dependency-injection - 与 MonoTouch 兼容的 IoC 库?

ios - 理解 subview.center = view.center 是如何工作的以及为什么 getter 和 setter 做不同的事情

c# - 如何检测循环中的任务