all. I have the following region I keep snippetting after the try {} blocks. It’s purpose is to provide debugging information whenever configured back to the database … It does debug nicely ( not all it is supposed to debug , but enough to find exactly the error, BUT ..
The code smells. Even I can get it. Do you have a better idea of error handling for db methods ( I am working currently on log4net implementation on it )
Here is the code:
region CatchExceptionsAdv5NoReturnAtAll
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
You should take a look at the exception handling block in Enterprise Library. It will let you do a lot of what you want to do without repeating such large code blocks. But, in general, why not factor out all the repeated code you have there for each exception. At a minimum, if you don’t want to use EntLib, factor out all the repeated code and build yourself a library of methods you can cleanly call from exception handling blocks to do what you want to do.