码迷,mamicode.com
首页 > 移动开发 > 详细

Catch Application Exceptions in a Windows Forms Application

时间:2014-04-27 21:10:16      阅读:907      评论:0      收藏:0      [点我收藏+]

标签:blog   class   com   code   img   http   div   java   javascript   style   tar   

 

 

You need to handle the System.Windows.Forms.Application.ThreadException event for Windows Forms. This article really helped me: http://bytes.com/forum/thread236199.html.

mamicode.com,码迷
Application.ThreadException += new ThreadExceptionEventHandler(MyCommonExceptionHandlingMethod)

private static void MyCommonExceptionHandlingMethod(object sender, ThreadExceptionEventArgs t)
{
    //Exception handling...
}
mamicode.com,码迷

AppDomain.UnhandledException and Application.ThreadException.

 

http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2

mamicode.com,码迷
using System;
using System.Security.Permissions;

public class Example 
{
   [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.ControlAppDomain)]
   public static void Main()
   {
      AppDomain currentDomain = AppDomain.CurrentDomain;
      currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

      try {
         throw new Exception("1");
      } catch (Exception e) {
         Console.WriteLine("Catch clause caught : {0} \n", e.Message);
      }

      throw new Exception("2");
   }

   static void MyHandler(object sender, UnhandledExceptionEventArgs args) 
   {
      Exception e = (Exception) args.ExceptionObject;
      Console.WriteLine("MyHandler caught : " + e.Message);
      Console.WriteLine("Runtime terminating: {0}", args.IsTerminating);
   }
}
mamicode.com,码迷

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Catch Application Exceptions in a Windows Forms Application,码迷,mamicode.com

Catch Application Exceptions in a Windows Forms Application

标签:blog   class   com   code   img   http   div   java   javascript   style   tar   

原文地址:http://www.cnblogs.com/zyip/p/3694683.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!