Search

Structured Exception Handling (SEH Class)

//Structured Exception Handling (SEH Class) 
//Usage: Choose the output type = SEH.OutType(MessageBox); 
//Throw using  (Exception ex,String Msg); 
     
    using System; 
     
    namespace SEH 
    { 
     public enum Output { Console=101, Debug, Messagebox, Dialog }; 
     
     static partial class SEH 
     { 
     
     private static int disp; 
     
     // Exception Output Display type 
     public static void OutType(Output val) 
     { 
     disp = Convert.ToInt32(val); 
     } 
     
     // Exception Data 
     public static void EX_Data(Exception EXC) 
     { 
     if (disp == 101) { System.Console.WriteLine(EXC.Data); } 
     if (disp == 102) { System.Diagnostics.Debug.Print("\nEXCEPTION Data: [ " + EXC.Data.ToString() + " ]"); } 
     if (disp == 103) 
     { 
     System.Windows.Forms.MessageBox.Show(" " + 
     EXC.Data, "EXCEPTION : [ Data ]", System.Windows.Forms.MessageBoxButtons.OK, 
     System.Windows.Forms.MessageBoxIcon.Hand); 
     } 
     } 
     
     // Exception Data with User Message 
     public static void EX_Data(Exception EXC, String Message) 
     { 
     if (disp == 101) { System.Console.WriteLine(EXC.Data + "\n \"" + Message + "\""); } 
     if (disp == 102) 
     { 
     System.Diagnostics.Debug.Print("\nEXCEPTION Data: [ " 
     + EXC.Data.ToString() + " ]" + "\n \"" + Message + "\""); 
     } 
     if (disp == 103) 
     { 
     System.Windows.Forms.MessageBox.Show(" " + 
     EXC.Data + "\n \"" + Message + "\"", "EXCEPTION : [ Data ]", 
     System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Hand); 
     } 
     } 
     
     // Exception Description 
     public static void EX_Msg(Exception EXC) 
     { 
     if (disp == 101) { System.Console.WriteLine(EXC.Message); } 
     if (disp == 102) 
     { 
     System.Diagnostics.Debug.Print( 
     "EXCEPTION Message: \"" + EXC.Message.ToString() + "\""); 
     } 
     if (disp == 103) 
     { 
     System.Windows.Forms.MessageBox.Show(" " + 
     EXC.Message, "EXCEPTION : [ Message ]", System.Windows.Forms.MessageBoxButtons. 
     OK, System.Windows.Forms.MessageBoxIcon.Information); 
     } 
     } 
     
     // Exception Description With Message 
     public static void EX_Msg(Exception EXC, string Message) 
     { 
     if (disp == 101) { System.Console.WriteLine(EXC.Message + "\n \"" + Message + "\""); } 
     if (disp == 102) 
     { 
     System.Diagnostics.Debug.Print( 
     "EXCEPTION Message: \"" + EXC.Message.ToString() + "\"" + "\n \"" + Message + "\""); 
     } 
     if (disp == 103) 
     { 
     System.Windows.Forms.MessageBox.Show(" " + 
     EXC.Message + "\n \"" + Message + "\"", 
     "EXCEPTION : [ Message ]", System.Windows.Forms.MessageBoxButtons. 
     OK, System.Windows.Forms.MessageBoxIcon.Information); 
     } 
     } 
     
     // Inner Exception 
     public static void EX_Inner(Exception EXC) 
     { 
     if (disp == 101) { System.Console.WriteLine(EXC.InnerException); } 
     if (disp == 102) 
     { 
     System.Diagnostics.Debug.Print("EXCEPTION InnerException: [ " + 
     EXC.InnerException.ToString() + " ]"); 
     } 
     if (disp == 103) 
     { 
     System.Windows.Forms.MessageBox.Show(" " + 
     EXC.InnerException, "EXCEPTION : [ Innerexception ]", 
     System.Windows.Forms.MessageBoxButtons.OK, 
     System.Windows.Forms.MessageBoxIcon.Information); 
     } 
     } 
     
     // Inner Exception with Message 
     public static void EX_Inner(Exception EXC, String Message) 
     { 
     if (disp == 101) { System.Console.WriteLine(EXC.InnerException + "\n \"" + Message + "\""); } 
     if (disp == 102) 
     { 
     if (EXC.InnerException != null) 
     System.Diagnostics.Debug.Print("EXCEPTION InnerException: [ " + 
     EXC.InnerException.ToString() + " ]" + "\n \"" + Message + "\""); 
     } 
     if (disp == 103) 
     { 
     System.Windows.Forms.MessageBox.Show(" " + 
     EXC.InnerException + "\n \"" + Message + "\"", 
     "EXCEPTION : [ Innerexception ]", 
     System.Windows.Forms.MessageBoxButtons.OK, 
     System.Windows.Forms.MessageBoxIcon.Information); 
     } 
     } 
     
     // Exception Help Link 
     public static void EX_Help(Exception EXC) 
     { 
     if (disp == 101) { System.Console.WriteLine(EXC.HelpLink); } 
     if (disp == 102) 
     { 
     System.Diagnostics.Debug.Print("EXCEPTION HelpLink: [ " + 
     EXC.HelpLink.ToString() + " ]"); 
     } 
     if (disp == 103) 
     { 
     System.Windows.Forms.MessageBox.Show(" " + 
     EXC.HelpLink, "EXCEPTION : [ Helplink ]", 
     System.Windows.Forms.MessageBoxButtons.OK, 
     System.Windows.Forms.MessageBoxIcon.Warning); 
     } 
     } 
     
     // Exception Help Link with user maeeage 
     public static void EX_Help(Exception EXC, string Message) 
     { 
     if (disp == 101) { System.Console.WriteLine(EXC.HelpLink + "\n \"" + Message + "\""); } 
     if (disp == 102) 
     { 
     if (EXC.HelpLink != null) 
     System.Diagnostics.Debug.Print("EXCEPTION HelpLink: [ " + EXC.HelpLink.ToString() + " ]" + "\n\" " + Message + "\""); 
     } 
     if (disp == 103) 
     { 
     System.Windows.Forms.MessageBox.Show(" " + 
     EXC.HelpLink + "\n \"" + Message + "\"", 
     "EXCEPTION : [ Helplink ]", 
     System.Windows.Forms.MessageBoxButtons.OK, 
     System.Windows.Forms.MessageBoxIcon.Warning); 
     } 
     } 
     
     // Exception Source 
     public static void EX_Source(Exception EXC) 
     { 
     if (disp == 101) { System.Console.WriteLine(EXC.Source); } 
     if (disp == 102) 
     { 
     System.Diagnostics.Debug.Print("EXCEPTION Source: [ " + 
     EXC.Source.ToString() + " ]"); 
     } 
     if (disp == 103) 
     { 
     System.Windows.Forms.MessageBox.Show(" " + 
     EXC.Source, "EXCEPTION : [ Source ]", 
     System.Windows.Forms.MessageBoxButtons.OK, 
     System.Windows.Forms.MessageBoxIcon.Asterisk); 
     } 
     } 
     
     // Exception Source with Message 
     public static void EX_Source(Exception EXC, string Message) 
     { 
     if (disp == 101) { System.Console.WriteLine(EXC.Source + "\n \"" + Message + "\""); } 
     if (disp == 102) 
     { 
     System.Diagnostics.Debug.Print("EXCEPTION Source: [ " + 
     EXC.Source.ToString() + " ]" + "\n \"" + Message + "\""); 
     } 
     if (disp == 103) 
     { 
     System.Windows.Forms.MessageBox.Show(" " + 
     EXC.Source + "\n \"" + Message + "\"", 
     "EXCEPTION : [ Source ]", 
     System.Windows.Forms.MessageBoxButtons.OK, 
     System.Windows.Forms.MessageBoxIcon.Asterisk); 
     } 
     } 
     
     // Dxception target 
     public static void EX_Target(Exception EXC) 
     { 
     if (disp == 101) { System.Console.WriteLine(EXC.TargetSite); } 
     if (disp == 102) 
     { 
     System.Diagnostics.Debug.Print("EXCEPTION TargetSize: [ " + 
     EXC.TargetSite.ToString() + " ]"); 
     } 
     if (disp == 103) 
     { 
     System.Windows.Forms.MessageBox.Show(" " + 
     EXC.TargetSite, 
     "EXCEPTION : [ TargetSize ]", 
     System.Windows.Forms.MessageBoxButtons.OK, 
     System.Windows.Forms.MessageBoxIcon.Error); 
     } 
     } 
     
     // Exception Target with Message 
     public static void EX_Target(Exception EXC, string Message) 
     { 
     if (disp == 101) { System.Console.WriteLine(EXC.TargetSite + "\n \"" + Message + "\""); } 
     if (disp == 102) 
     { 
     System.Diagnostics.Debug.Print("EXCEPTION TargetSize: [ " + 
     EXC.TargetSite.ToString() + " ]" + "\n \"" + Message + "\""); 
     } 
     if (disp == 103) 
     { 
     System.Windows.Forms.MessageBox.Show(" " + 
     EXC.TargetSite + "\n \"" + Message + "\"", 
     "EXCEPTION : [ TargetSize ]", 
     System.Windows.Forms.MessageBoxButtons.OK, 
     System.Windows.Forms.MessageBoxIcon.Error); 
     } 
     } 
     
     // Exception Trace 
     public static void EX_Trace(Exception EXC) 
     { 
     if (disp == 101) { System.Console.WriteLine(EXC.StackTrace); } 
     if (disp == 102) 
     { 
     System.Diagnostics.Debug.Print("EXCEPTION StackTrace: [ " + 
     EXC.StackTrace.ToString() + " ]"); 
     } 
     if (disp == 103) 
     { 
     System.Windows.Forms.MessageBox.Show(" " + 
     EXC.StackTrace, "EXCEPTION : [ StackTrace ]", 
     System.Windows.Forms.MessageBoxButtons.OK, 
     System.Windows.Forms.MessageBoxIcon.Stop); 
     } 
     } 
     
     // Exception tace with Message 
     public static void EX_Trace(Exception EXC, string Message) 
     { 
     if (disp == 101) { System.Console.WriteLine(EXC.StackTrace + "\n \"" + Message + "\""); } 
     if (disp == 102) 
     { 
     System.Diagnostics.Debug.Print("EXCEPTION StackTrace: [ " + 
     EXC.StackTrace.ToString() + " ]" + "\n \"" + Message + "\""); 
     } 
     if (disp == 103) 
     { 
     System.Windows.Forms.MessageBox.Show(" " + 
     EXC.StackTrace + "\n \"" + Message + "\"", 
     "[" + Message + "]", 
     System.Windows.Forms.MessageBoxButtons.OK, 
     System.Windows.Forms.MessageBoxIcon.Stop); 
     } 
     } 
     
     // All Exception 
     public static void EX_All(Exception EXC) 
     { 
     if (disp == 101) { System.Console.WriteLine(EXC.ToString()); } 
     if (disp == 102) { System.Diagnostics.Debug.Print("EXCEPTION StackTrace: [ " + EXC.ToString() + " ]"); } 
     if (disp == 103) 
     { 
     System.Windows.Forms.MessageBox.Show(" " + 
     EXC.ToString(), 
     "EXCPTION : [ INFO ]", 
     System.Windows.Forms.MessageBoxButtons.OK, 
     System.Windows.Forms.MessageBoxIcon.Information); 
     } 
     } 
     
     //Catches every type of exception(shows TypeInfo) 
     public static void _typo(Exception EXC) 
     { 
     
     System.Windows.Forms.MessageBox.Show(" " + 
     EXC.GetType(), //+  @"\nHashCode:"+ EXC.GetHashCode(), 
     "EXCPTION : [" + EXC.GetType() + "]", 
     System.Windows.Forms.MessageBoxButtons.OK, 
     System.Windows.Forms.MessageBoxIcon.Information); 
     } 
     
     
     } 
    }