Jim McKeeth
Jim McKeeth

Reputation: 38703

How to have an exception define its own message in C#?

I want to define a custom exception that has two special properties: Field and FieldValue, and I want the message to be built from those two values in the exception constructor. Unfortunately the Message is read only.

This is what I have, but it still requires the message to be passed.

    public class FieldFormatException: FormatException
    {
        private Fields _field;
        private string _fieldValue;
        public Fields Field{ get{ return _field; } }
        public string FieldValue { get { return _value; } }
        public FieldFormatException() : base() { }
        private FieldFormatException(string message) { }
        public FieldFormatException(string message, Fields field, string value): 
            base(message)
        {
            _fieldValue = value;
            _field = field;               
        }
        public FieldFormatException(string message, Exception inner, Fields field, string value): 
            base(message, inner)
        {
            _fieldValue = value;
            _field = field;
        }
        protected FieldFormatException(System.Runtime.Serialization.SerializationInfo info,
              System.Runtime.Serialization.StreamingContext context): base(info, context){}
    }

How can I remove the Message as a parameter from the constructor, and then set the message based on the value of Field and FieldValue?

Upvotes: 10

Views: 9063

Answers (6)

Mark Byers
Mark Byers

Reputation: 838046

Override it:

    public override string Message
    {
        get
        {
            return string.Format("My message: {0}, {1}", Field, FieldValue);
        }
    }

As discussed in the comments, even though you said that you didn't want a message in the constructor, you may want to consider allowing users to optionally pass their own message to the constructor of your exception and having that displayed as well.

Upvotes: 18

DOK
DOK

Reputation: 32831

I always add a property to my custom exceptions so that I can change the message contents. Actually, I add two properties: one for the message text to be displayed to the user (DisplayMessage), and another for logging many details about the exception (LogMessage) such as user information, the details of a sproc call, data entry details, and so on.

Then, you can leave the Message property alone.

Upvotes: 2

womp
womp

Reputation: 116977

Can you not just call the base constructor by constructing your message inline? This would leave message out of it entirely.

 public FieldFormatException(string field, string value): 
         base(field.ToString() + value.ToString())
 {
    _fieldValue = value;
    _field = field;               
 }

Upvotes: 4

Richard
Richard

Reputation: 108975

You can override the Message property.

Or create a static helper method:

private static string MakeMessage(....) {
  return "Message from parameters of this method";
}

And use that in the base class constructor call:

public FileFormatException(string field, string fieldValue)
  : base(MakeMessage(field, fieldValue) { ... }

Upvotes: 3

Stefan Steinegger
Stefan Steinegger

Reputation: 64628

Not sure if I understand your question, but what about this?

    public FieldFormatException(Fields field, string value): 
        base(BuildMessage(field, value))
    {
    }

    private static string BuildMessage(Fields field, string value)
    {
       // return the message you want
    }

Upvotes: 25

Mark Seemann
Mark Seemann

Reputation: 233135

You could override Exception.Message (it's virtual).

Upvotes: 2

Related Questions