我试图捕获 C++/CLI 应用程序中的所有异常,以便我可以记录它们(包括堆栈跟踪)。到目前为止,我有一些看起来很有希望的代码:
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
try
{
Application::Run(gcnew MainForm());
}
catch( System::Exception^ e )
{
String^ message = gcnew String("");
System::Exception^ exceptionRecursor = e;
message = "ERROR: Uncaught exception encountered!\n\n";
while( exceptionRecursor )
{
message += exceptionRecursor->Source+"\n\t";
message += exceptionRecursor->Message+"\n\t";
message += exceptionRecursor->StackTrace+"\n\n";
exceptionRecursor = exceptionRecursor->InnerException;
}
MessageBox::Show(message);
}
return 0;
}
...但是我没有显示一个包含我整理的错误的对话框,而是得到了其他内容:
An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
Additional information: Exception has been thrown by the target of an invocation.
这是因为Run
命令正在尝试以某种方式处理异常?我需要处理里面的事情吗MainForm
某处? ...或者是否有其他(更好)的方法来解决这个问题。
暂时忘记错误的来源(我正处于开发周期中期,仍在调试),如果能够捕获这些错误并生成一个整洁的小堆栈跟踪,该跟踪可以保留在代码中直到部署和部署,那就太好了当出现问题时让用户知道。最终我将错误报告包装成可以通过网络报告的内容。
如果反射发生在不同的线程中,则包装器不会捕获它的失败。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)