WinFormsで使用するもの:
- System.Windows.Forms.Application.ThreadException
- System.Windows.Application.UnhandledException
Winforms以外のマルチスレッドアプリケーションには何を使用すればよいですか?
以下のC#.NET4.0の完全なコードを検討してください。
using System;
using System.Threading.Tasks;
namespace ExceptionFun
{
class Program
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Task.Factory.StartNew(() =>
{
throw new Exception("Oops, someone forgot to add a try/catch block");
});
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
//never executed
Console.WriteLine("Logging fatal error");
}
}
}
私はstackoverflowについて同様の質問をたくさん見ましたが、満足のいく答えが含まれているものはありませんでした。ほとんどの回答は、「コードに適切な例外処理を含める必要があります」または「AppDomain.CurrentDomain.UnhandledExceptionを使用する」というタイプです。
編集:私の質問は誤解されていたようですので、それを再定式化し、より小さなコード例を提供しました。