为什么catch块永远不会在设备上执行?

时间:2012-12-21 23:20:58

标签: 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 (别担心这个名字 - 当我发现这个时,我正在调查一个不同的崩溃)。

2 个答案:

答案 0 :(得分:2)

我刚刚发现它只在启用了LLVM的情况下发生 也许这是一个错误所以我会把它报告给Xamarin。

有趣的是,添加

BeginInvokeOnMainThread (() => {
    new UIAlertView ("Gotta catch 'em all!", "", null, "OK", null).Show ();
});
Console.WriteLine呼叫帮助后

我看到警报和日志条目。

我的sample project

答案 1 :(得分:1)

也许是一个愚蠢的问题但是满足条件要求使用catch命令?

如果是这样,应用程序可能会在返回“Gotcha”之前中断并且iOS命令会接管。

我相信iOS会监听您的命令/应用程序是否以及何时中断,因为默认函数会启动以处理此问题。与dealloc命令类似。