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
どこかで物事を処理する必要がありますか? ...または、これについて他の (より良い) 方法があります。
ちょっとの間、エラーの原因を忘れて (私は開発サイクルの途中で、まだデバッグ中です)、これらのエラーをトラップして、配置までコードに残るきちんとした小さなスタック トレースを生成できると便利です。問題が発生したときにユーザーに知らせます。最終的には、エラー レポートを Web 経由でレポートできるものにラップします。