Click here to Skip to main content
15,868,141 members
Articles / General Programming / Exceptions

Exception Handling Using Custom Attributes and Stacktrace

Rate me:
Please Sign up or sign in to vote.
4.76/5 (9 votes)
25 Nov 2013CPOL2 min read 46.4K   363   15   9
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)


Written By
Software Developer (Senior) cognizant
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionthanks Pin
plgiacaouytin16-Oct-22 16:51
professionalplgiacaouytin16-Oct-22 16:51 
QuestionPerformance Pin
FatCatProgrammer10-Jan-14 5:07
FatCatProgrammer10-Jan-14 5:07 
QuestionThanks for this Pin
Jared Leonard2-Dec-13 8:34
Jared Leonard2-Dec-13 8:34 
AnswerRe: Thanks for this Pin
Balachandar Jeganathan2-Dec-13 13:34
professionalBalachandar Jeganathan2-Dec-13 13:34 
GeneralFor certain use-cases only Pin
John Brett24-Nov-13 23:35
John Brett24-Nov-13 23:35 
GeneralRe: For certain use-cases only Pin
Balachandar Jeganathan25-Nov-13 2:36
professionalBalachandar Jeganathan25-Nov-13 2:36 
QuestionProblems Pin
Alois Kraus23-Nov-13 20:27
Alois Kraus23-Nov-13 20:27 
AnswerRe: Problems Pin
Balachandar Jeganathan25-Nov-13 2:29
professionalBalachandar Jeganathan25-Nov-13 2:29 
You are correct; in release mode 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

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.