Setting the message of a custom Exception without passing it to the base constructor

后端 未结 5 1285
悲&欢浪女
悲&欢浪女 2021-02-11 12:20

I want to make a custom Exception in C#, but in theory I do need to do a little parsing first before I can make a human readable ExceptionMessage.

The problem is that th

相关标签:
5条回答
  • 2021-02-11 12:31

    I think the problem may be with the Visual Studio debugger. I got the same exact results you got using the debugger, but when I print the message instead:

    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);
            }
        }
    }
    

    I get the expected "Show this message".

    If you put a breakpoint where the Exception is caught, the debugger does show you the correct message.

    0 讨论(0)
  • 2021-02-11 12:41

    What's wrong with something like this.

        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)
        { }
    
    }
    

    I just use a string and include parameters. Simple solution.

    0 讨论(0)
  • 2021-02-11 12:45

    Consider the Microsoft Guidelines for creating new exceptions:

      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);
        }
      }
    

    Note the static factory method (not part of the pattern), that you can use in your program like this:

    throw CustomException.FromJson(variable);
    

    That way you followed best practice and can parse your json inside the exception class.

    0 讨论(0)
  • 2021-02-11 12:50

    Just put the formatting code into a static method?

    public CustomException(dynamic json) : base(HumanReadable(json)) {}
    private static string HumanReadable(dynamic json) {
        return whatever you need to;
    }
    
    0 讨论(0)
  • 2021-02-11 12:56

    I like to use this here. It is easy and does not need the static function:

    public class MyException : Exception
    {
        public MyException () : base("This is my Custom Exception Message")
        {
        }
    }
    
    0 讨论(0)
提交回复
热议问题