Best error handler code for debugging db related methods in C #?

all. I have the following section, which I leave after the try {} snippet. The goal is to provide debug information whenever it is configured back into the database ... It debugs great (not all of them need to be debugged, but enough to find the error for sure, BUT ..

The code smells. Even I can get it. Do you have a better understanding of error handling for db methods (I am currently working on a log4net implementation)

Here is the code:

region CatchExceptionsAdv5NoReturnAtAllAll

    catch (NullReferenceException nre)
    {
        if (userObj == null)
        {
            userObj = new FB.User ();
            FB.User.GiveDefaultUser ( ref userObj );
        }

        System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
            encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += nre.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
            userObj.Mc.Msg = "Failed to debug application error at " + methodName;
            //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

    } //eof catch

    catch (System.InvalidOperationException ioe) //comm -- occurs when no result set was found !!!
    {
        if (userObj == null)
        {
            userObj = new FB.User ();
            FB.User.GiveDefaultUser ( ref userObj );
        }

        System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
            encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ioe.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
            userObj.Mc.Msg = "Failed to debug application error at " + methodName;
            //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

    } //eof catch (System.InvalidOperationException)

    catch (System.IndexOutOfRangeException ioore) //comm -- occurs when no result set was found !!!
    {
        if (userObj == null)
        {
            userObj = new FB.User ();
            FB.User.GiveDefaultUser ( ref userObj );
        }

        System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
            encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ioore.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
            userObj.Mc.Msg = "Failed to debug application error at " + methodName;
            //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

    } //eof catch (System.IndexOutOfRangeException)

    catch (System.Data.SqlClient.SqlException sqle)
    {
        if (userObj == null)
        {
            userObj = new FB.User ();
            FB.User.GiveDefaultUser ( ref userObj );
        }

        System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
            encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += sqle.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
            userObj.Mc.Msg = "Failed to debug application error at " + methodName;
            //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

    } //eof catch

    catch (System.FormatException fe)
    {
        if (userObj == null)
        {
            userObj = new FB.User ();
            FB.User.GiveDefaultUser ( ref userObj );
        }

        System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
            encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += fe.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
            userObj.Mc.Msg = "Failed to debug application error at " + methodName;
            //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

    } //eof catch

    catch (Exception ex)
    {
        if (userObj == null)
        {
            userObj = new FB.User ();
            FB.User.GiveDefaultUser ( ref userObj );
        }

        System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
            encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ex.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
            userObj.Mc.Msg = "Failed to debug application error at " + methodName;
            //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

    } //eof catch
    #endregion CatchExceptionsAdv5NoReturnAtAll

      

0


a source to share


2 answers


You should take a look at the exception handling block in the Enterprise Library . This allows you to do a lot of the things you want to do without having to repeat such large blocks of code. But, in general, why not separate out all the duplicate code you have for each exception. At a minimum, if you don't want to use EntLib, correct all the repetitive code and create yourself a library of methods that you can call cleanly from the exception handling blocks to do what you want to do.



+2


a source


I want the real answer to be either using the Enterprise Library or log4net ... Meanwhile, a quick and dirty refactoring way was to replace the region with the following call:

  catch (Exception ex)
  {
    System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace ();
    Utils.ErrorHandler.Trap ( ref userObj, st, ex );
    return false;
  } //eof catch

      



which pointed to the below error handler class, which does not improve error reporting, does not provide a better architecture for error reporting ... as long as it just removed duplicate code and improved readability:

using System;

namespace Utils
{
  public class ErrorHandler
  {


    public static void Trap ( ref FB.User userObj, System.Diagnostics.StackTrace st, Exception ex )
    {
      if (userObj == null)
      {
        userObj = new FB.User ();
        FB.User.GiveDefaultUser ( ref userObj );
      }


      if (ex is NullReferenceException)
      {
        if (userObj == null)
        {
          userObj = new FB.User ();
          FB.User.GiveDefaultUser ( ref userObj );
        }


        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
          encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ex.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
          userObj.Mc.Msg = "Failed to debug application error at " + methodName;
          //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

      } //eof if

      if (ex is System.InvalidOperationException) //comm -- occurs when no result set was found !!!
      {
        if (userObj == null)
        {
          userObj = new FB.User ();
          FB.User.GiveDefaultUser ( ref userObj );
        }


        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
          encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ex.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
          userObj.Mc.Msg = "Failed to debug application error at " + methodName;
          //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

      } //eof if

      if (ex is System.IndexOutOfRangeException) //comm -- occurs when no result set was found !!!
      {
        if (userObj == null)
        {
          userObj = new FB.User ();
          FB.User.GiveDefaultUser ( ref userObj );
        }


        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
          encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ex.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
          userObj.Mc.Msg = "Failed to debug application error at " + methodName;
          //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

      } //eof if

      if (ex is System.Data.SqlClient.SqlException)
      {
        if (userObj == null)
        {
          userObj = new FB.User ();
          FB.User.GiveDefaultUser ( ref userObj );
        }


        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
          encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ex.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
          userObj.Mc.Msg = "Failed to debug application error at " + methodName;
          //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

      } //eof if

      if (ex is System.FormatException)
      {
        if (userObj == null)
        {
          userObj = new FB.User ();
          FB.User.GiveDefaultUser ( ref userObj );
        }


        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
          encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ex.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
          userObj.Mc.Msg = "Failed to debug application error at " + methodName;
          //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

      } //eof if

      if (ex is Exception)
      {
        if (userObj == null)
        {
          userObj = new FB.User ();
          FB.User.GiveDefaultUser ( ref userObj );
        }


        string methodName = st.GetFrame ( 1 ).GetMethod ().Name;
        string className = st.GetFrame ( 1 ).GetFileName ();
        int lineNumber = st.GetFrame ( 1 ).GetFileLineNumber ();

        string encryptedErrorCode = String.Empty;
        encryptedErrorCode += "className - " + className + " methodName ";
        encryptedErrorCode += methodName + " lineNumber " + lineNumber.ToString ();
        encryptedErrorCode += userObj.DomainName;

        if (System.Convert.ToInt16 ( BL.Conf.Instance.Vars["EncryptErrorMessages"] ) == 1)
          encryptedErrorCode = Utils.DataEncryption.EncryptString ( encryptedErrorCode, userObj.DomainName );


        userObj.Mc.Msg = "An error in the application occurred. Report the following error code " + encryptedErrorCode;
        userObj.Mc.ClassName += className + " ; ";
        userObj.Mc.MethodName += methodName + " ; ";
        userObj.Mc.DebugMsg += ex.Message;
        //
        if (Providers.nsDbMeta.DbDebugger.DebugAppError ( ref userObj ) == false)
        {
          userObj.Mc.Msg = "Failed to debug application error at " + methodName;
          //Providers.nsDbMeta.Providers.nsDbMeta.DbDebugger.WriteIf ( userObj.Mc.Msg );
        }

      } //eof catch


    } //eof method 



  }//eof class CustomException
} //eof namespace U

      

0


a source







All Articles