C# 异常未给出预期的消息

C# 异常未给出预期的消息,c#,asp.net,exception,C#,Asp.net,Exception,我已经创建了一个自定义异常,但是当它被触发时,我没有得到我期望的消息 根据下面抛出的异常,“UIButtons”部分不包含元素键为“Save”的元素,而不是未处理的执行错误消息,我希望在自定义类中触发重写 有人能帮我诊断一下为什么会发生这种情况吗 using System; namespace Payntbrush.Infrastructure.Application.Exceptions.Configuration { [Serializable] public class

我已经创建了一个自定义异常,但是当它被触发时,我没有得到我期望的消息

根据下面抛出的异常,“UIButtons”部分不包含元素键为“Save”的元素,而不是
未处理的执行错误
消息,我希望在自定义类中触发重写

有人能帮我诊断一下为什么会发生这种情况吗

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;
            }
        }
    }
}
当它开火时,我得到了这个信息

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: 

我认为您应该将
get
消息覆盖中的
if
语句更改为:

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

目前,如果构造时提供的消息为null或空,那么您将返回null/empty-因此框架在报告异常时将自己填充

如果消息为null/empty,您可能想提供自己的消息

次要更新-咨询

顺便说一句,我通常在构造函数中做这种事情:

public MyException(string message) : base(message ?? "Default message") { }
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;   
}   
在您的情况下,我只需要一个构造函数就可以这样做:

public MyException(string message) : base(message ?? "Default message") { }
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;   
}   

另外,不要忘记受保护的构造函数和
GetObjectData
覆盖,以确保可以正确保存/加载和封送异常。

在输出窗口中从何处获取该消息?您知道“UIButtons”部分不包含元素键为“Save”的元素”Break English是对的吗?如果总是改变
消息的值而不是按你现在的方式去做,不是更容易吗?