473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 1565
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Carsten Gehling | last post by:
> -----Oprindelig meddelelse----- > Fra: python-list-admin@python.org > På vegne af David McNab > Sendt: 17. juli 2003 01:32 > Til: python-list@python.org > Emne: Re: Choosing the right...
11
by: Jean de Largentaye | last post by:
Hi, I need to parse a subset of C (a header file), and generate some unit tests for the functions listed in it. I thus need to parse the code, then rewrite function calls with wrong parameters....
5
by: Henrik J | last post by:
Hello group..! I have this struct: struct FooData { unsigned short *foolist; int NoOfFoos; }; I want to dynamicly allocate X numbers of my FooData struct.
4
by: Jonas Hei | last post by:
I need to decided between Standard and Enterprise Edition (Cost is a criteria - but its secondary to performance - <!--and I am not paying for it myself-->) The server spec under consideration:...
1
by: will | last post by:
why doesnt this work? Im basically trying to get the value of the root node attribute the 'Result' attr. Which can be one of 3 things. OK, ERROR or INVALID, and then choosing between the 3 and...
15
by: Ant | last post by:
Hi, This might seem like a strange question but I'm wondering how other developers go about choosing the appropriate Exception objects to use in their catch statements. Currently, I choose them...
25
by: David Sanders | last post by:
Hi, As part of a simulation program, I have several different model classes, ModelAA, ModelBB, etc., which are all derived from the class BasicModel by inheritance. model to use, for example...
19
by: hansBKK | last post by:
Upfront disclaimer - I am a relative newbie, just starting out learning about PHP, mostly by researching, installing and playing with different scripts. I am looking for a host that will provide...
28
by: Rico Secada | last post by:
Hi. First let me start by saying, please don't let this become a flame-thing. Second, I need some advice. I am a 35 year old programmer, who program in C/C++, PHP and Bourne Shell almost...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.