Choosing an exception

G

Guest

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
 
T

Truong Hong Thi

How should you worry about this as it always cause a compile-time error?
 
C

Chris Springer

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
 
G

Guest

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
 
T

Truong Hong Thi

That you mean *parse* an int from a string.
See documentation of Int32.Parse(string).
 
G

Guest

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
 
T

Truong Hong Thi

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
 

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