Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / exceptions

Exception Handling Using Custom Attributes and Stacktrace

4.76/5 (9 votes)
25 Nov 2013CPOL2 min read 48.3K   365  
Exception handling using custom attributes and stacktrace.

Introduction

Exception handling is a very important part in every application. It should tell the proper message to the end user; so that they can explain to the supporting team properly. It will be very helpful to identify and resolve the issue quickly. 

Background

I was filling one application form then I submitted it. But it prompted one generic message that 'There is a technical issue, you can not precede further, please contact administrator'. I do not understand what the exact problem is. If the application prompted an actual message, I could have understood. So I developed a small utility which generates the actual failure message using stacktrace and custom attribute. Here, we do not need to implement try catch block for all the methods.

To implement this approach, we need to place a custom attribute on top of all the methods and it should describe what the method actually does so that we can generate message for each step while exception occurred.

Where we can use

Since this utility extract the method description from the entire StackTrace, it will disclose the business flow to the end user. It is not required and it may be a loophole for hackers; so that this approach will be helpful for in-house tool where performing complex algorithms also if you do not want to disclose any action, we no need to specify the description for the particular method.

Known issue

In release build we cannot have all the steps. If you really required all the steps, we can get it by changing the build configuration settings.

  • Project-> Properties -> Build
  • Configuration = Release
  • Optimized Code = false (unchecked)
  • Advanced build settings: Debug Info: full

Source Code

The main application is as follows:

C#
class Program
{
    static void Main(string[] args)
    {
        try
        {
            var loanValidator = new LoanValidator();
            loanValidator.CheckLoanApplication();
        }
        catch (Exception ex)
        {
            string exceptionMessage=ExceptionHelper.GetDetailMessage(ex);
            Console.Write(exceptionMessage);
            Console.Read();                
        }
    }
}

The output is as given below:

Sample Image

Custom Attribute source code is as follows:

C#
public class MethodDescription : Attribute
{
    public MethodDescription(string description)
    {
        this.Description = description;
    }
    public string Description { get; set; }
}

Loan validator source code is given below:

C#
public class LoanValidator
{
    [MethodDescription("Validating loan application")]
    public bool CheckLoanApplication()
    {
        bool isValidPersonalDetail = CheckPersonalDetail();
        bool isValidEmploymentDetail = CheckEmploymentDetail();
        bool isValidBankAccountDetail = CheckBankAccountDetail();

        if (isValidPersonalDetail && 
        isValidEmploymentDetail && isValidBankAccountDetail)
            return true;
        else
            return false;
    }

    [MethodDescription("Checking personal detail")]
    private bool CheckPersonalDetail()
    {
        return true;
    }

    [MethodDescription("Checking employment detail")]
    private bool CheckEmploymentDetail()
    {
        return true;
    }
    [MethodDescription("Checking bank account detail")]
    private bool CheckBankAccountDetail()
    {
        CheckAnyOutstanding();
        return true;
    }

    [MethodDescription("Checking outstanding detail from common web server")]
    private bool CheckAnyOutstanding()
    {
        throw new WebException();
        return true;
    }
}

Exception handler helper source code is shown below:

C#
public class ExceptionHelper
{
    /// <summary>
    /// Frame the detail message from exception
    /// </summary>
    /// <param name="ex"></param>
    /// <returns></returns>
    public static string GetDetailMessage(Exception ex)
    {
        StringBuilder messageBuilder = new StringBuilder();
        Type attributeType = typeof(MethodDescription);            
        
        int step = 1;

        var attributes = GetStackTraceSteps<MethodDescription>(ex);
        if(attributes!=null && attributes.Count>0)
        messageBuilder.AppendLine(string.Format
        ("Sorry there is a problem while processing step {0}:",attributes.Count));
        foreach (var attribute in attributes)
        {
            messageBuilder.Append(string.Format
            ("Step {0}: {1}", step.ToString(), attribute.Description));
            messageBuilder.AppendLine();
            step++;
        }
        messageBuilder.AppendLine();

        string formatedMessage = string.Format("{0}Error Description : {1}",
                                                messageBuilder.ToString(),
                                                ex.Message
                                                );

        return formatedMessage;
    }

    /// <summary>
    /// Extrace the custom attribute details from the stacktrace
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="ex"></param>
    /// <returns></returns>
    public static List<T> GetStackTraceSteps<T>(Exception ex)
    {
        List<T> traceSteps = new List<T>();
        Type attributeType = typeof(T);
        StackTrace st = new StackTrace(ex);
        if (st != null && st.FrameCount > 0)
        {

            for (int index = st.FrameCount - 1; index >= 0; index--)
            {
                var attribute = st.GetFrame(index).
                GetMethod().GetCustomAttributes(attributeType, false).FirstOrDefault();
                if (attribute != null)
                {
                    traceSteps.Add((T)attribute);

                }
            }
        }

        return traceSteps;
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)