Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- void Run()
- {
- AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
- Console.WriteLine("Press enter to exit.");
- do
- {
- (new Thread(delegate()
- {
- throw new ArgumentException("ha-ha");
- })).Start();
- } while (Console.ReadLine().Trim().ToLowerInvariant() == "x");
- Console.WriteLine("last good-bye");
- }
- int r = 0;
- void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
- {
- Interlocked.Increment(ref r);
- Console.WriteLine("handled. {0}", r);
- Console.WriteLine("Terminating " + e.IsTerminating.ToString());
- Thread.CurrentThread.IsBackground = true;
- Thread.CurrentThread.Name = "Dead thread";
- while (true)
- Thread.Sleep(TimeSpan.FromHours(1));
- //Process.GetCurrentProcess().Kill();
- }
- static void Main(string[] args)
- {
- Console.WriteLine("...");
- (new Program()).Run();
- }
- }
- try
- {
- CallTheCodeThatMightThrowException()
- }
- catch (Exception ex)
- {
- System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
- Utils.ErrorHandler.Trap ( ref objUser, st, ex );
- } //eof catch
- using System;
- using log4net; //or another logging platform
- namespace GenApp.Utils
- {
- public class ErrorHandler
- {
- public static void Trap ( Bo.User objUser, ILog logger, System.Diagnostics.StackTrace st, Exception ex )
- {
- if (ex is NullReferenceException)
- {
- //do stuff for this ex type
- } //eof if
- if (ex is System.InvalidOperationException)
- {
- //do stuff for this ex type
- } //eof if
- if (ex is System.IndexOutOfRangeException)
- {
- //do stuff for this ex type
- } //eof if
- if (ex is System.Data.SqlClient.SqlException)
- {
- //do stuff for this ex type
- } //eof if
- if (ex is System.FormatException)
- {
- //do stuff for this ex type
- } //eof if
- if (ex is Exception)
- {
- //do stuff for this ex type
- } //eof catch
- } //eof method
- }//eof class
- } //eof namesp
- Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
- Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Add Comment
Please, Sign In to add comment