Finding the non-value part of ArgumentException.Message

Viewed 83

An ArgumentException with a non-trivial ParamName has a newline split Message whose final line describes its ParamName; for example, new ArgumentException("Some error", "myParam").Message is

Some error
Parameter name: myParam

Now, while using a library which we'll consider black box for now, and which produces ArgumentExceptions with very useful error messages, I find myself wanting to take those error messages, add some context to them, and eventually present the information to my user. In doing so, I want to get rid of the "Parameter name" part of the exception message as that tends to be useful only during development.

So the question is: How do I get rid of the line containing "Parameter name" in the most elegant way? The ParamName property of ArgumentException allows me to get the parameter name itself, but there appears to be no direct way to get a message that excludes the parameter name. One thing I could do would be to simply take all lines of Message except from the last one, but that only works insofar that ParamName is not null; I can check this explicitly, but at this point I'm performing a bunch of parsing to solve what seems like the kind of thing that would be a .NET one-liner.

1 Answers
Related