72

我想在 C# 中创建一个自定义异常,但理论上我确实需要先进行一些解析,然后才能创建一个人类可读的异常消息。

问题是原始 Message 只能通过调用 的基础构造函数来设置Messsage,所以我无法提前进行任何解析。

我尝试像这样覆盖 Message 属性:

public class CustomException : Exception
{
    string _Message;

    public CustomException(dynamic json) : base("Plep")
    {
        // Some parsing to create a human readable message (simplified)
        _Message    = json.message;
    }

    public override string Message
    {
        get { return _Message; }
    }
}

问题是 Visual Studio 调试器仍然显示我已传递给构造函数的消息,在本例中为Plep

throw new CustomException( new { message="Show this message" } )

结果是:

Visual Studio 异常对话框

如果我将基本构造函数留空,它将显示一条非常通用的消息:

App.exe 中出现“App.CustomException”类型的未处理异常

问题

看起来异常对话框读取了一些我也无权访问的字段/属性。是否有任何其他方法可以在 Exception 的基本构造函数之外设置人类可读的错误消息。

请注意,我使用的是 Visual Studio 2012。

4

5 回答 5

123

只是将格式化代码放入静态方法中?

public CustomException(dynamic json) : base(HumanReadable(json)) {}
private static string HumanReadable(dynamic json) {
    return whatever you need to;
}
于 2013-07-24T10:34:33.060 回答
24

考虑创建新例外的 Microsoft 指南:

  using System;
  using System.Runtime.Serialization;

  [Serializable]
  public class CustomException : Exception
  {
    //
    // For guidelines regarding the creation of new exception types, see
    //    https://msdn.microsoft.com/en-us/library/ms229064(v=vs.100).aspx
    //

    public CustomException()
    {
    }

    public CustomException(string message) : base(message)
    {
    }

    public CustomException(string message, Exception inner) : base(message, inner)
    {
    }

    protected CustomException(SerializationInfo info, StreamingContext context) : base(info, context)
    {
    }

    public static CustomException FromJson(dynamic json)
    {
      string text = ""; // parse from json here

      return new CustomException(text);
    }
  }

请注意静态工厂方法(不是模式的一部分),您可以像这样在程序中使用它:

throw CustomException.FromJson(variable);

这样您就可以遵循最佳实践,并且可以在异常类中解析您的 json。

于 2013-07-17T09:12:27.783 回答
9

我认为问题可能出在 Visual Studio 调试器上。我得到了与使用调试器相同的结果,但是当我打印消息时:

class CustomException : Exception {
    public CustomException(dynamic json)
        : base("Plep") {
            _Message = json.message;
    }

    public override string Message {
        get { return _Message; }
    }

    private string _Message;
}

class Program {
    static void Main(string[] args) {
        try {
            throw new CustomException(new { message = "Show this message" });
        } catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }
}

我得到了预期"Show this message"

如果您在捕获异常的位置设置断点,调试器会向您显示正确的消息。

于 2013-07-24T13:03:17.483 回答
7

这样的事情有什么问题。

    public class FolderNotEmptyException : Exception
{

    public FolderNotEmptyException(string Path) : base($"Directory is not empty. '{Path}'.")
    { }

    public FolderNotEmptyException(string Path, Exception InnerException) : base($"Directory is not empty. '{Path}'.", InnerException)
    { }

}

我只是使用一个字符串并包含参数。简单的解决方案。

于 2018-12-20T13:50:17.450 回答
3

我喜欢在这里使用它。这很简单,不需要静态函数:

public class MyException : Exception
{
    public MyException () : base("This is my Custom Exception Message")
    {
    }
}
于 2018-03-03T18:54:00.780 回答