我有一个简单的C#应用程序/游戏使用SFML.Net,目前停止运行/终止后1或2秒钟内被执行没有任何警告,异常等。考虑以下代码:
public static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);
--> The following two lines are run
Console.WriteLine("Hello");
Console.WriteLine("Please don't go...");
// Run the game
try
{
--> This line is reached
Game.Run();
}
catch (Exception e)
{
--> This line is never reached
Console.WriteLine(e.Message.ToString());
}
--> These lines are never reached
Console.WriteLine("Noone ever comes here, I feel so lonely... :(");
Console.ReadKey();
}
}此时,您可能预期Game.Run()方法有问题。奇怪的是,根据VS调试器,方法的第一行从未到达。
public static void Run()
{
try
{
--> Never reached
LoadContent();
// Do stuff here
while (window.IsOpen())
{
// The classic game loop
}
}
catch (Exception e)
{
--> Never reached
Console.WriteLine(e.Message.ToString());
Console.ReadKey();
}
}发布于 2013-12-15 02:41:04
几个月后,我又回到了这个问题上,意识到我的错误是多么愚蠢。显然,默认情况下未启用非托管代码调试(这是我第一次使用VS2012),此异常是从SFML的基本C++库抛出的。
对于那些不知道的人,启用非托管调试:项目>属性> Debug >选择“启用非托管代码调试”
发布于 2013-09-08 02:24:56
我的猜测是:
Access Violation Exception,不能被抓住。或
Run方法之前,还有其他一些异常。为了验证这一点,请确保调试器在第一次机会异常时停止。如果您指定:
发布于 2013-09-08 03:55:10
我猜异常是由另一个线程抛出的。无法捕获主线程中的另一个线程引发的异常。所以你在catch中的断点永远不会被击中。顺便说一句,您是否尝试过在UnhandledExceptionEventHandler方法OnUnhandledException中有一个断点?这就是所有未处理的异常都会发生的地方!希望这能有所帮助。
https://stackoverflow.com/questions/18679743
复制相似问题