473,756 Members | 8,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

try catch - "Variable declared but never used" ?

In the following code, "catch" is underlined in blue, and the error "the
variable fx is declared but never used" is displayed when I mouse hover over
it.

Am I doing something wrong, or do I just ignore it?

private void btnCalc_Click(o bject sender, System.EventArg s e)

{

try

{

int intNum = Int32.Parse(txt NumEntry.Text);

long lngNum = Int64.Parse(txt NumEntry.Text);

}

catch (System.FormatE xception fx) //Non integer input

{

MessageBox.Show ("Invalid entry!", "Entry Error!",

MessageBoxButto ns.OK, MessageBoxIcon. Exclamation);

}

finally

{

//unexpected error

}

}

Thanks - Sean
Feb 27 '06 #1
7 14120
If you aren't going to use it, then don't reference it

catch (System.FormatE xception)

--
Robbe Morris - 2004-2006 Microsoft MVP C#
http://www.eggheadcafe.com/forums/merit.asp

"What-A-Tool" <Di************ *****@DieDieDie .Com> wrote in message
news:6AtMf.5336 9$Ug4.7762@duke read12...
In the following code, "catch" is underlined in blue, and the error "the
variable fx is declared but never used" is displayed when I mouse hover
over it.

Am I doing something wrong, or do I just ignore it?

private void btnCalc_Click(o bject sender, System.EventArg s e)

{

try

{

int intNum = Int32.Parse(txt NumEntry.Text);

long lngNum = Int64.Parse(txt NumEntry.Text);

}

catch (System.FormatE xception fx) //Non integer input

{

MessageBox.Show ("Invalid entry!", "Entry Error!",

MessageBoxButto ns.OK, MessageBoxIcon. Exclamation);

}

finally

{

//unexpected error

}

}

Thanks - Sean

Feb 27 '06 #2
What-A-Tool a écrit :
In the following code, "catch" is underlined in blue, and the error "the
variable fx is declared but never used" is displayed when I mouse hover over
it.

Am I doing something wrong, or do I just ignore it?

private void btnCalc_Click(o bject sender, System.EventArg s e)

{

try

{

int intNum = Int32.Parse(txt NumEntry.Text);

long lngNum = Int64.Parse(txt NumEntry.Text);

}

catch (System.FormatE xception fx) //Non integer input

{

MessageBox.Show ("Invalid entry!", "Entry Error!",

MessageBoxButto ns.OK, MessageBoxIcon. Exclamation);

}

finally

{

//unexpected error

}

}

Thanks - Sean


You can write the following line into your catch block :
fx.ToString();
there won't be any warning anymore and you will have the possibility to
look at the error at debug time if you want

Bob
Feb 27 '06 #3

"Boblemar" <ar************ *@logicim.com> wrote in message
news:uM******** ******@TK2MSFTN GP09.phx.gbl...

You can write the following line into your catch block :
fx.ToString();
there won't be any warning anymore and you will have the possibility to
look at the error at debug time if you want

Bob


Simple solution :-)

Just seemed strange, cause I coded it the way I saw in several different
examples, and nothing was ever mentioned about errors being pointed.

Thanks - Sean
Feb 27 '06 #4
This is just a warning. You can change your error level at the compler
option.

"What-A-Tool" <Se**@IHateSpam .Com> wrote in message
news:QdBMf.5338 5$Ug4.43518@duk eread12...

"Boblemar" <ar************ *@logicim.com> wrote in message
news:uM******** ******@TK2MSFTN GP09.phx.gbl...

You can write the following line into your catch block :
fx.ToString();
there won't be any warning anymore and you will have the possibility to
look at the error at debug time if you want

Bob


Simple solution :-)

Just seemed strange, cause I coded it the way I saw in several different
examples, and nothing was ever mentioned about errors being pointed.

Thanks - Sean

Feb 27 '06 #5
Sandeep <Sa*****@mailin ator.com> wrote:
This is just a warning. You can change your error level at the compler
option.


True, but it's far better to clean up the warning in the way suggested
by Robbe. Turning off warnings may well cause grief when you come up
against a problem which would have normally raised a warning.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 27 '06 #6
This is a personal bugbear of mine. It's a very useful warning in the
case of local variables, but it's very annoying in the case of "catch".
In the case of "catch," if you eliminate the variable name, then as
Boblemar points out you can't see the exception at debug time: you can
put a breakpoint in the catch body, but you can't see what exception
was caught.

I usually use some stupid trick like Boblemar's in order to fool the
compiler into shutting up, but I wish that variables declared in
"catch" clauses were an exception to this message.

Feb 27 '06 #7
Bruce Wood <br*******@cana da.com> wrote:
This is a personal bugbear of mine. It's a very useful warning in the
case of local variables, but it's very annoying in the case of "catch".
In the case of "catch," if you eliminate the variable name, then as
Boblemar points out you can't see the exception at debug time: you can
put a breakpoint in the catch body, but you can't see what exception
was caught.

I usually use some stupid trick like Boblemar's in order to fool the
compiler into shutting up, but I wish that variables declared in
"catch" clauses were an exception to this message.


One alternative to actually calling something at runtime would be to
call a method which was conditionally removed - i.e.

DevNullUtil.DoN othingWith(e)

where DoNothingWith had a conditional attribute which would never be
defined.

Not something I've ever done myself, but just a thought...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 27 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
5742
by: harry | last post by:
Hi, During compilation, a C# project in my solution triggers the following warning: "warning CS0168: The variable 'ex' is declared but never used" To trigger this warning, it appears the C# compiler WarningLevel needs to be 3 or above.
2
2033
by: ~~~ .NET Ed ~~~ | last post by:
It is not the first time I see this happen. I am using VS.NET 2003 with .NET Framework 1.1. In this particular situation I have a custom user control in a windows form. There is a member variable declared with the fully qualified class name (some.namespace.mycontrolclass). Then in the InitializeComponent() it is instantiated and there is a block of code lines that set the various properties of the control class, then further down the usual...
3
3947
by: Pierre | last post by:
Hello, In an aspx page (mypage.aspx) from a web projet, I would like to get the value of a variable of the projet that is declared as public in a module. The variable can be called from anywhere in the code behind page and in the class files, but when I try to call it from an aspx page, it raises an error. <%=myvar%> in mypage.aspx raise an error : the name "myvar" is not declared.
11
2236
by: gg9h0st | last post by:
i saw a code refactorying onload event listener window.onloadListeners=new Array(); window.addOnLoadListener=function(listener) { window.onloadListeners=listener; } why declare the onloadListeners, addOnLoadListener below window?
3
2018
by: Yansky | last post by:
If I have the following code: var abc; if(!abc){ alert('test'); }
0
9275
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
9873
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...
0
9713
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
8713
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...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
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.