Lameth's Blog
Just another WordPress.com weblog

C# Exception Handling, unhandeld Exceptions

[CSharp] using System; using System.Diagnostics; using System.Windows.Forms; class Application { static void Main() { // Register an Event Handler whitch // handeles the unhandeled Exception AppDomain.CurrentDomain.UnhandeledException += new UnhandeledExceptionEventHandler(OnUnhandeledExceptionPolicy); try { // Application code comes here } finally { // Finalisation code comes here } } static void OnUnhandeledExceptionPolicy(Object Sender, UnhandeledExceptionEventArgs e) { String InformationForLogging; Exception ex = e.ExceptionObject as Exception; if(ex != null) { // The unhandeled Exception is CLS compliant // Extract the Information you want to know InformationForLogging = ex.ToString(); } else { // The unhandeled Exception is not CLS compliant // You can only handle this Exception as Object InformationForLogging = String.Format( “Type: {0}{2}String: {1}”, e.ExceptionObject.GetType(), e.ExceptionObject.ToString(), Environment.NewLine) } #if DEBUG if(!e.IsTerminating) { // Exception occurred in a thread pool or finalizer thread // Debugger launches only explicitly } else { // Exception occurred in managed thread // Debugger will also launch when not launched explicitly } // explicitly launch the debugger (Visual Studio) Debugger.Launch(); #else // This is a final release; // logging is done to Syslog and over the Web if possible // Debugger is mostly not available if(!e.IsTerminating) { // Exception occurred in a thread pool or finalizer thread // Application keeps open } else { // Exception occurred in managed thread // Application is closing, so you should log now } #endif } }

Exception classes at C# Online.NET (CSharp-Online.NET)

No Responses Yet to “C# Exception Handling, unhandeld Exceptions”

Leave a Reply