I’m trying to find all of our code that re-throws exceptions where the new exception being thrown does not contain the original exception as the inner exception. Here’s an example:
catch(DBApplicationException dbEx)
{
BaseApplicationException bEx = new BaseApplicationException(dbEx.Message, dbEx);
bEx.MethodName = System.Reflection.Assembly.GetExecutingAssembly().FullName + "::" + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString();
bEx.Severity = ExceptionSeverityLevel;
PublishingManager.Publish(bEx,"");
throw bEx;
}
catch(Exception e)
{
BaseApplicationException bEx = new BaseApplicationException(e.Message);
bEx.MethodName = System.Reflection.Assembly.GetExecutingAssembly().FullName + "::" + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString();
bEx.Severity = ExceptionSeverityLevel;
PublishingManager.Publish(bEx,"");
throw bEx;
}
The first catch (catch (DBApplicationException dbEx) gets re-thrown as BaseApplicationException, but as you can see, it sets the message as dbEx.Message, and then specifies the InnerException as dbEx, but the second catch segment re-throws without the InnerException, it only contains the e.Message.
So for my regex pattern, I only want to find the entire catch block that doesn’t contain the inner exception, right now, the pattern I’m using returns both of these two catch blocks together.
Here’s my regex pattern:
catch((.|\n|\r)*){((.|\n|\r)*)Exception\(((?!,).)+\);((.|\n|\r)*)}
Here’s my method block to test this scenario:
public static DataSet SearchUserSessions(string username, string first, string last, string startDate, string endDate)
{
DataSet ds = null;
try
{
SqlParameter [] arParms = new SqlParameter[]
{
new SqlParameter("@UserName", username),
new SqlParameter("@FirstName", first),
new SqlParameter("@LastName", last),
new SqlParameter("@SessionStart", startDate),
new SqlParameter("@SessionEnd", endDate)
};
DB db = new DB();
ds = db.ExecuteDataset(SecurityConfig.ConnectionString, CommandType.StoredProcedure,
SPSearchUserSessions, (DB.Provider)SecurityConfig.ConnectionProviderType,
arParms);
}
catch(DBApplicationException dbEx)
{
BaseApplicationException bEx = new BaseApplicationException(dbEx.Message, dbEx);
bEx.MethodName = System.Reflection.Assembly.GetExecutingAssembly().FullName + "::" + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString();
bEx.Severity = ExceptionSeverityLevel;
PublishingManager.Publish(bEx,"");
throw bEx;
}
catch(Exception e)
{
BaseApplicationException bEx = new BaseApplicationException(e.Message);
bEx.MethodName = System.Reflection.Assembly.GetExecutingAssembly().FullName + "::" + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString();
bEx.Severity = ExceptionSeverityLevel;
PublishingManager.Publish(bEx,"");
throw bEx;
}
return ds;
}
This works against your example:
But I agree that it’s not a maintainable way of tackling this – unless that’s a one-off check.