AppDomain.UnhandledException イベント
アセンブリ: mscorlib (mscorlib.dll 内)

Dim instance As AppDomain Dim handler As UnhandledExceptionEventHandler AddHandler instance.UnhandledException, handler
public: virtual event UnhandledExceptionEventHandler^ UnhandledException { void add (UnhandledExceptionEventHandler^ value) sealed; void remove (UnhandledExceptionEventHandler^ value) sealed; }

このイベントは、キャッチされない例外の通知を提供します。このイベントにより、システムの既定のハンドラが例外をユーザーに報告し、アプリケーションを終了する前に、アプリケーションで例外に関する情報を記録できます。アプリケーションの状態に関する十分な情報を使用できる場合、後で回復に使用するためにプログラム データを保存するなど、その他のアクションが実行される場合もあります。未処理の例外により、プログラム データが回復できないほど破損する可能性があるため、警告が発信されます。
![]() |
---|
.NET Framework Version 1.0 および 1.1 では、このイベントが発生する前に、アプリケーションの終了とデバッグ オプションがユーザーに報告されます。 |
このイベントは、任意のアプリケーション ドメインで処理できます。既定のアプリケーション ドメインで処理される場合、このイベントは、すべてのアプリケーション ドメイン内のすべての未処理の例外に対して発生します。子ドメインで処理される場合、このイベントは、そのドメイン内の未処理の例外に対してだけ発生します。イベントが既定のドメインと子ドメインの両方で処理される場合、子ドメインの未処理の例外により、両方のドメインでこのイベントが発生します。
![]() |
---|
.NET Framework Version 1.0 および 1.1 では、このイベントは、アプリケーションの起動時にシステムによって作成されたアプリケーション ドメインに対してだけ発生します。アプリケーションが追加のアプリケーション ドメインを作成する場合、それらの追加アプリケーション ドメインでこのイベントのデリゲートを指定しても無効です。 |
.NET Framework Version 1.0 および 1.1 では、メイン アプリケーション スレッド以外のスレッドで発生した未処理の例外は、ランタイムによってキャッチされたため、アプリケーションは終了しませんでした。したがって、アプリケーションを終了しなくても、UnhandledException イベントが発生しました。.NET Framework Version 2.0 では、このような応答のない障害が累積するとパフォーマンスの低下、データの破損、およびロックアップを招き、これらはすべてデバッグが難しいため、子スレッドの未処理の例外用のこのバックストップは削除されました。詳細については、「マネージ スレッドの例外」を参照してください。
このイベントのイベント ハンドラを登録するには、適切なアクセス許可が必要です。アクセス許可がないと、SecurityException がスローされます。

UnhandledException イベントのサンプルを次に示します。
Sub Main() Dim currentDomain As AppDomain = AppDomain.CurrentDomain AddHandler currentDomain.UnhandledException, AddressOf MyHandler Try Throw New Exception("1") Catch e As Exception Console.WriteLine("Catch clause caught : " + e.Message) End Try Throw New Exception("2") ' Output: ' Catch clause caught : 1 ' MyHandler caught : 2 End Sub 'Main Sub MyHandler(sender As Object, args As UnhandledExceptionEventArgs) Dim e As Exception = DirectCast(args.ExceptionObject, Exception) Console.WriteLine("MyHandler caught : " + e.Message) End Sub 'MyUnhandledExceptionEventHandler
using System; using System.Security.Permissions; public class Test { [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlAppDomain)] public static void Example() { AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler); try { throw new Exception("1"); } catch (Exception e) { Console.WriteLine("Catch clause caught : " + e.Message); } throw new Exception("2"); // Output: // Catch clause caught : 1 // MyHandler caught : 2 } static void MyHandler(object sender, UnhandledExceptionEventArgs args) { Exception e = (Exception) args.ExceptionObject; Console.WriteLine("MyHandler caught : " + e.Message); } public static void Main() { Example(); } }
public ref class Test { private: static void MyHandler( Object^ /*sender*/, UnhandledExceptionEventArgs^ args ) { Exception^ e = dynamic_cast<Exception^>(args->ExceptionObject); Console::WriteLine( "MyHandler caught : {0}", e->Message ); } public: [SecurityPermissionAttribute( SecurityAction::Demand, ControlAppDomain = true )] static void Main() { AppDomain^ currentDomain = AppDomain::CurrentDomain; currentDomain->UnhandledException += gcnew UnhandledExceptionEventHandler( Test::MyHandler ); try { throw gcnew Exception( "1" ); } catch ( Exception^ e ) { Console::WriteLine( "Catch clause caught : {0}", e->Message ); } throw gcnew Exception( "2" ); // Output: // Catch clause caught : 1 // MyHandler caught : 2 } }; int main() { Test::Main(); }


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


_AppDomain.UnhandledException イベント
アセンブリ: mscorlib (mscorlib.dll 内)

Event UnhandledException As UnhandledExceptionEventHandler
Dim instance As _AppDomain Dim handler As UnhandledExceptionEventHandler AddHandler instance.UnhandledException, handler
event UnhandledExceptionEventHandler^ UnhandledException { void add (UnhandledExceptionEventHandler^ value); void remove (UnhandledExceptionEventHandler^ value); }


Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- AppDomain.UnhandledExceptionのページへのリンク