ArgumentOutOfRangeException

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am throwing the ArgumentOutOfRangeException in my code as shown below

throw new ArgumentOutOfRangeException("value", "Value must be > 0");

my catch block is below

catch(ArgumentOutOfRangeException exception)
{
MessageBox.Show(exception.Message,"Input Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);

} // end catch

My question is the message box shows both my custom error of "Value must be
0. and paramamter name=value. Why is it displaying parameter name when all
I am requesting is the exception.Message? Thanks for your help.
 
Simply because that is how ArgumentOutOfRangeException chooses to
format it's inner message when a parameter name is supplied; can't
really do much about it unless you a: don't supply the param-name, b:
reformat it, or c: catch the specific type and display your own
message:

string actualMsg =
new ArgumentOutOfRangeException("ParamName",
"MyMessage").Message;

gives:
MyMessage[crlf]
Parameter name: ParamName

Marc
 
Back
Top