未提供预期消息的异常

本文关键字:消息 异常 | 更新日期: 2023-09-27 18:29:46

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

我期望的不是Unhandled Execution Error消息,而是自定义类中的重写,它将是基于下面抛出的异常的The section 'UIButtons' does not contain an element with element key '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;
            }
        }
    }
}

要扔它,我正在使用:

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

当它启动时,我收到这个消息

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: 

未提供预期消息的异常

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

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

注意!

目前,如果构造时提供的消息为null或为空,那么您将返回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;   
}   

另外,不要忘记受保护的构造函数和GetObjectData重写,以确保可以正确地保存/加载和整理异常。