ArgumentOutOfRangeException

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.
 
M

Marc Gravell

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top