473,659 Members | 2,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1577
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.FormatEx ception so you could
do something like this...

try
{
....
}
catch (System.FormatE xception exception)
{
MessageBox(exce ption.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*@discussion s.microsoft.com > wrote in message
news:EA******** *************** ***********@mic rosoft.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.FormatEx ception so you could
do something like this...

try
{
....
}
catch (System.FormatE xception exception)
{
MessageBox(exce ption.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*@discussion s.microsoft.com > wrote in message
news:EA******** *************** ***********@mic rosoft.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(str ing).

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(str ing).

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
1683
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 framework > I've been working on an HTML generation framework called 'pyWeb'. Just > posted to this ng yesterday, calling for testers. >
11
9599
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. What I call "shaking the broken tree" :) I chose to make my UT-generator in Python 2.4. However, I am now encountering problems in choosing the right parser for the job. I struggle in choosing between the inappropriate, the out-of-date, the...
5
5487
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
2377
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: Dual Xeon, 1GB RAM, 36GB - RAID 1 (Dell PowerEdge 1850). Application: Windows 2003 Std Server, ASP.NET, MS SQL Server 2000 based data driven web application.
1
1603
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 outputting different stuff. It always prints out "OtherwiseHasBeenSelected" suggesting it doesnt test true on any of my choices : xsl: <?xml version='1.0' encoding='iso-8859-1'?> <xsl:stylesheet version='1.0'...
15
1436
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 only when they are returned in the error message of an unhandled exception, then set a general exeption handler to deal with anything else that might happen. This doesn't seem the best way, but maybe it is(?). Should I get to know the names of all...
25
2137
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 if the parameter model_name is "aa", then choose ModelAA. Currently I do this as follows:
19
2569
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 the right environment for this - running a wide variety of PHP applications. I realise that security is also important, but for now flexibility is more important to me. Note that I'm **not** looking for people to recommend hosting companies, I...
28
1905
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 daily.
0
8332
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8746
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8525
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2750
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.