471,337 Members | 1,134 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Choosing an exception

Ant
Hi,
I'd like to catch a specific exception that is raised when an int is
assigned a string. I can't force the exception to see what is thrown because
it creates a compile time error when fed a dummy value.
What exception would I choose to do this. (to catch a wrong assignment
exception, specifically string value to int type)?

Thanks for your thoughts in advance
Ant
Dec 2 '05 #1
6 1498
How should you worry about this as it always cause a compile-time error?

Dec 2 '05 #2
If you try to assign an int to a string...then yes you will get a compile
time error. I'm guessing that you're trying to catch the exception raised
when people enter invalid data through a dialog interface. I think what
you're trying to catch would be of type System.FormatException so you could
do something like this...

try
{
....
}
catch (System.FormatException exception)
{
MessageBox(exception.Message);
....additional handing...
}

hope this helps...

Chris
--
Securing your systems is much like fighting off disease -- as long as you
maintain basic hygiene, you're likely to be okay, but you'll never be
invulnerable.

Steve Shah - Unix Systems Network Administrator
"Ant" <An*@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.com...
Hi,
I'd like to catch a specific exception that is raised when an int is
assigned a string. I can't force the exception to see what is thrown
because
it creates a compile time error when fed a dummy value.
What exception would I choose to do this. (to catch a wrong assignment
exception, specifically string value to int type)?

Thanks for your thoughts in advance
Ant

Dec 2 '05 #3
Ant
Thank you, that should do it.

I'm building a class with no current UI available to me & it will probably
have to catch input errors that I can't test for now.

Thanks very much for that.
Ant

"Chris Springer" wrote:
If you try to assign an int to a string...then yes you will get a compile
time error. I'm guessing that you're trying to catch the exception raised
when people enter invalid data through a dialog interface. I think what
you're trying to catch would be of type System.FormatException so you could
do something like this...

try
{
....
}
catch (System.FormatException exception)
{
MessageBox(exception.Message);
....additional handing...
}

hope this helps...

Chris
--
Securing your systems is much like fighting off disease -- as long as you
maintain basic hygiene, you're likely to be okay, but you'll never be
invulnerable.

Steve Shah - Unix Systems Network Administrator
"Ant" <An*@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.com...
Hi,
I'd like to catch a specific exception that is raised when an int is
assigned a string. I can't force the exception to see what is thrown
because
it creates a compile time error when fed a dummy value.
What exception would I choose to do this. (to catch a wrong assignment
exception, specifically string value to int type)?

Thanks for your thoughts in advance
Ant


Dec 2 '05 #4
That you mean *parse* an int from a string.
See documentation of Int32.Parse(string).

Dec 2 '05 #5
Ant
Thanks for the idea Truong but in fact I don't want to parse the value. I
want to stop it altogether. Only numerical values can be used.
Alphanumericals must be caught in an exception handler. I don't want to parse
1df4 into an int & in fact that's not possible anyway but thanks for the idea.

Regards
Ant

"Truong Hong Thi" wrote:
That you mean *parse* an int from a string.
See documentation of Int32.Parse(string).

Dec 2 '05 #6
I am sorry about being strict, but if you don't want to parse, then you
never need to worry at all - because the compiler does not allow such
assignment. Besides, if you assign an int to an int, you never need to
catch because it never raises exception.

Because you did not clearly describle your problem, I suppose it is
like this: you want to be sure a value (a string) entered through UI
(or read from file/DB, etc.) to be a valid representation of an
integer. The thing you worry is that the string entered might not
represent a valid integer. No matter whether it represents a valid
integer or not, it *is* a string, and you no way can assign it directly
to a variable declared to be of type int. In any cases, you have to
*convert* the string to an int. Thus, you need to use Int32.Parse (or
Convert.ToInt32 method). If the string represents a valid int, then no
problem. If not, exception occurs as described in the documentation of
the method.

It seems you are not a experienced C# programmer, so let's think about
that :)

Thi

Dec 2 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Carsten Gehling | last post: by
11 posts views Thread by Jean de Largentaye | last post: by
5 posts views Thread by Henrik J | last post: by
4 posts views Thread by Jonas Hei | last post: by
15 posts views Thread by Ant | last post: by
25 posts views Thread by David Sanders | last post: by
28 posts views Thread by Rico Secada | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.