3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C# 例外処理(Try-Catch)がないところで出るエラーを全て一纏めにキャッチする

Posted at

C#でtry-catchが記載されていない箇所でエラーが起きても
アプリが落ちないようにする。

C#では、
「Application.ThreadException」イベントが
用意されている。

使い方
Program.cs(コピペ用)

static class Program
{
    /// <summary>
    /// アプリケーションのメイン エントリ ポイントです。
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.ThreadException += Application_ThreadException;

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
    {
        MessageBox.Show(e.Exception.Message);
    }
}

画像
キャプチャ.PNG

参考資料:
■適切に処理されなかった例外をキャッチするには?
 https://www.atmarkit.co.jp/ait/articles/0507/01/news134.html

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?