例外処理

.net Frameworkでのエラー処理

1.グローバルエラーハンドラ

グローバルエラーハンドラを追加することによって未処理の例外を処理することが可能になる。
方法は「Windowsフォームアプリケーション」と「それ以外(コンソールアプリケーション、マルチスレッド)」の場合で異なる。

1.コンソールアプリケーションの場合

1.グローバルエラーハンドラの追加
Application.ThreadException += new
    ThreadExceptionEventHandler(Application_ThreadException);

     // UnhandledExceptionイベント・ハンドラを登録する
     Thread.GetDomain().UnhandledException += new
       UnhandledExceptionEventHandler(Application_UnhandledException);

2.未処理例外を処理するメソッドの追加
public static void Application_ThreadException(object sender,  ThreadExceptionEventArgs e)
{
     ShowErrorMessage(e.Exception, "Application_ThreadExceptionによる例外通知です。");
}

2.それ以外のアプリケーションの場合

1.アプリケーションのメイン関数内にグローバルエラーハンドラの追加
Thread.GetDomain().UnhandledException 
 += new UnhandledExceptionEventHandler(Application_UnhandledException);
2.未処理例外を処理するメソッドを追加
private static void 
 Application_UnhandledException(object sender, UnhandledExceptionEventArgs e){
 ...............
}



2.ADO.netにおける例外処理

ADO.netを利用してデータアクセスを行った場合の例外処理は以下の二つのパターンに分けられる。

  1. Severityが10より小さい場合の処理
  2. Severityが10以上の場合の処理

3.参考リンク


Edited By Karai 2008年02月28日


最終更新:2008年02月28日 01:49