1

I have created a custom exception however when it is fired, I'm not getting the message I expect.

Instead of the Unhandled Execution Error message I am expecting the override in my custom class to fire which would be The section 'UIButtons' does not contain an element with element key 'Save' based on the exception being thrown below.

Can anyone help me diagnose why this is happening?

using System;

namespace Payntbrush.Infrastructure.Application.Exceptions.Configuration
{
    [Serializable]
    public class ConfigElementDoesNotExistException : Exception
    {
        private string _sectionName;
        private string _elementName;

        public ConfigElementDoesNotExistException(string sectionName, string elementName, string errorMessage = "")
                             : base(errorMessage)
        {
            _sectionName = sectionName;
            _elementName = elementName;
        }

        public ConfigElementDoesNotExistException(string sectionName, string elementName, string errorMessage, Exception innerEx)
                             : base(errorMessage, innerEx)
        {
            _sectionName = sectionName;
            _elementName = elementName;
        }

        /// <summary>
        /// Gets a message that describes the current exception
        /// </summary>
        public override string Message
        {
            get
            {
                string exceptionMessage;

                if (String.IsNullOrEmpty(base.Message))
                {
                    exceptionMessage = base.Message;
                }
                else
                {
                    exceptionMessage = String.Format("The section '{0}' does not contain an element with element key '{1}'", _sectionName, _elementName);
                }

                return exceptionMessage;
            }
        }
    }
}

To throw it I am using:

throw new ConfigElementDoesNotExistException("UIButtons", "Save");

And when it fires I get this message

Unhandled Execution Error

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: Payntbrush.Infrastructure.Application.Exceptions.Configuration.ConfigElementDoesNotExistException: 
3
  • where do you get that message, in the output window? Commented Apr 3, 2012 at 11:04
  • You do know that " The section 'UIButtons' does not contain an element with element key 'Save' " is broken English right? Commented Apr 3, 2012 at 11:19
  • Wouldn't it be easier to just always change the value of Message instead of doing it the way you are? Commented Apr 3, 2012 at 11:23

1 Answer 1

1

I think you should change the if statement in your get override of Message to:

if (!String.IsNullOrEmpty(base.Message)) 

Note the !.

At present, if the message supplied on construction is null or empty, then you're returning null/empty - so the framework is filling in it's own when reporting the exception.

Presumably you meant to supply your own message if it's null/empty.

Minor update - advisory

Incidentally - I generally do this kind of thing in the constructor:

public MyException(string message) : base(message ?? "Default message") { }

In your case I would do it like this with just one constructor:

public ConfigElementDoesNotExistException(string sectionName, 
                                          string elementName, 
                                          string errorMessage = null, 
                                          Exception innerEx = null)   
: base(errorMessage ?? string.Format("The section {0} doesn't ... {1}", 
                       sectionName ?? "[unknown]", elementName ?? "[unknown]")
       , innerEx)   
{   
  _sectionName = sectionName;   
  _elementName = elementName;   
}   

Also don't forget the protected constructor and GetObjectData override to ensure your exception can be saved/loaded and marshalled properly.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.