zoukankan      html  css  js  c++  java
  • Catch Application Exceptions in a Windows Forms Application

    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.

    Application.ThreadException += new ThreadExceptionEventHandler(MyCommonExceptionHandlingMethod)
    
    private static void MyCommonExceptionHandlingMethod(object sender, ThreadExceptionEventArgs t)
    {
        //Exception handling...
    }
    
    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

    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} 
    ", 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);
       }
    }
  • 相关阅读:
    hadoop再次集群搭建(3)-如何选择相应的hadoop版本
    48. Rotate Image
    352. Data Stream as Disjoint Interval
    163. Missing Ranges
    228. Summary Ranges
    147. Insertion Sort List
    324. Wiggle Sort II
    215. Kth Largest Element in an Array
    快速排序
    280. Wiggle Sort
  • 原文地址:https://www.cnblogs.com/zyip/p/3694683.html
Copyright © 2011-2022 走看看