C# Tutorial - Dealing With Unhandled Exceptions

转载至【 http://www.switchonthecode.com/tutorials/csharp-tutorial-dealing-with-unhandled-exceptions 】

 

It's a fact of life that all applications have bugs- even after strenuous bench testing, automated testing, and piles and piles of test plans. When you release software into the wild, something is going to go wrong, somewhere. So it is nice to have an underlying architecture in your program so that when something does go horribly, unavoidably wrong, there is at least some record of what happened - so you can start tracking down and reproducing it. Today we are going to talk about an extremely simple way to do just that in C#.

Generally, since we are in the managed world of .NET and C#, when something horrible goes wrong with your program, it will eventually manifest itself as an unhandled exception - and then the app will crash and burn. Now, I suppose you could try and surround all your code with generic try-catch statements, but (I'm hoping) we all know that that is just a horrible idea, from performance, maintenance, and code readability perspectives. Fortunately, there is a better way to implement that idea in .NET - the UnhandledException event.

The UnhandledException is an event on the AppDomain class. An AppDomain is, in very simplistic terms, the execution environment for your code. We are going to assume today that there is only one AppDomain, and we aren't going to worry about it any more than that. Talking about AppDomains could probably take an entire tutorial in and of itself. But anyway, we have this event - what does it do and how do we attach to it?

What it does is pretty simple - the event is fired every time there is an unhandled exception that propagates all the way to the top of your application. Now, if there was nothing attached to the event, the app would show one of those classic "this app has encountered a problem and needs to close" dialogs, and down your app would go. But once you attach to this event, you have a chance to do something.

How to attach to the event is a little weird - but only in that you may have never added code to this part of a C# app before - especially if what you mostly work with are GUI apps. We need to add a line to the Main method (by default in the "Program.cs" file in a new Windows Application):

 

using System ;
using System.Collections.Generic ;
using System.Windows.Forms ;

namespace MyApp
{
  static class Program
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread ]
    static void Main ( )
    {
      AppDomain. CurrentDomain. UnhandledException +=
        new UnhandledExceptionEventHandler (CurrentDomain_UnhandledException ) ;
      Application. EnableVisualStyles ( ) ;
      Application. SetCompatibleTextRenderingDefault ( false ) ;
      Application. Run ( new Form1 ( ) ) ;
    }
  }
}

 

That is the content of a default "Program.cs" file for a new Visual Studio Windows Application called "MyApp" - with one difference. The line of code AppDomain.CurrentDomain.Unhandled... is what I added in order to attach to the UnhandledException event of my AppDomain. Of course, here it is referencing a function that does not yet exist (CurrentDomain_UnhandledException) - but we will get to that next.

Ok, lets write that method:

 

static void CurrentDomain_UnhandledException
  ( object sender, UnhandledExceptionEventArgs e )
{
  try
  {
    Exception ex = ( Exception )e. ExceptionObject ;

    MessageBox. Show ( "Whoops! Please contact the developers with the following"
          + " information:/n/n" + ex. Message + ex. StackTrace,
          "Fatal Error", MessageBoxButtons. OK, MessageBoxIcon. Stop ) ;
  }
  finally
  {
    Application. Exit ( ) ;
  }
}

 

And there you go - now your unhandled exceptions are being shown in a nice happy dialog! Of course, you could do other things here - like log the exception, or try and soften the blow of the crash by saving whatever program state you can. You can't, however, actually keep the program from crashing - there is no way to "catch" the exception at this point and let the app keep on running.

But wait, theres more! As I stated before, with the UnhandledException event on AppDomain, any exception in the current AppDomain will trigger that event. This includes not only the main application thread (the UI thread), but any other threads as well. And, as I just mentioned, there is no way to "recover" once you hit that point. However, there is actually a different way to handle unhandled exceptions on the Application thread (i.e., the thread that is handling your UI - forms, painting/ user input, etc..). This is through the ThreadException event on Application. To do this, we add another line to the main method:

 

Application. ThreadException +=
  new System. Threading. ThreadExceptionEventHandler (Application_ThreadException ) ;

 

And we define the function Application_ThreadException:

 

public static void Application_ThreadException
  ( object sender, System. Threading. ThreadExceptionEventArgs e )
{
  DialogResult result = DialogResult. Abort ;
  try
  {
    result = MessageBox. Show ( "Whoops! Please contact the developers with the"
      + " following information:/n/n" + e. Exception. Message + e. Exception. StackTrace,
      "Application Error", MessageBoxButtons. AbortRetryIgnore, MessageBoxIcon. Stop ) ;
  }
  finally
  {
    if (result == DialogResult. Abort )
    {
      Application. Exit ( ) ;
    }
  }
}

 

With this method hooked to the Application.ThreadException, unhandled exceptions on the main application thread will not hit the UnhandledException event on the AppDomain - and the app will no longer terminate by default. As you can see in this method, we show a dialog asking if the user wants to continue or not - and if they choose abort, we close the app. Otherwise, we just let the app continue on.

Here is what the file "Program.cs" looks like now:

 

using System ;
using System.Collections.Generic ;
using System.Windows.Forms ;

namespace MyApp
{
  static class Program
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread ]
    static void Main ( )
    {
      AppDomain. CurrentDomain. UnhandledException +=
        new UnhandledExceptionEventHandler (CurrentDomain_UnhandledException ) ;
      Application. ThreadException +=
        new System. Threading. ThreadExceptionEventHandler (Application_ThreadException ) ;
      Application. EnableVisualStyles ( ) ;
      Application. SetCompatibleTextRenderingDefault ( false ) ;
      Application. Run ( new Form1 ( ) ) ;
    }

    static void CurrentDomain_UnhandledException
        ( object sender, UnhandledExceptionEventArgs e )
    {
      try
      {
        Exception ex = ( Exception )e. ExceptionObject ;

        MessageBox. Show ( "Whoops! Please contact the developers with "
            + "the following information:/n/n" + ex. Message + ex. StackTrace,
            "Fatal Error", MessageBoxButtons. OK, MessageBoxIcon. Stop ) ;
      }
      finally
      {
        Application. Exit ( ) ;
      }
    }

    public static void Application_ThreadException
      ( object sender, System. Threading. ThreadExceptionEventArgs e )
    {
      DialogResult result = DialogResult. Abort ;
      try
      {
        result = MessageBox. Show ( "Whoops! Please contact the developers "
          + "with the following information:/n/n" + e. Exception. Message
          + e. Exception. StackTrace, "Application Error",
          MessageBoxButtons. AbortRetryIgnore, MessageBoxIcon. Stop ) ;
      }
      finally
      {
        if (result == DialogResult. Abort )
        {
          Application. Exit ( ) ;
        }
      }
    }
  }
}

 

In any decent sized application, the code in either of these two methods would probably be much more complicated - logging much more state information, trying to determine if the app could continue at all, so on and so forth. But hopefully this gives you an idea of where to put that sort of logic in a C# application. As always, if you have questions, please feel free to leave them in the comments.

 

 

=========================================================================

Visual C++
=========================================================================

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();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值