By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,854 Members | 876 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,854 IT Pros & Developers. It's quick & easy.

try catch - "Variable declared but never used" ?

P: n/a
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(object sender, System.EventArgs e)

{

try

{

int intNum = Int32.Parse(txtNumEntry.Text);

long lngNum = Int64.Parse(txtNumEntry.Text);

}

catch (System.FormatException fx) //Non integer input

{

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

MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

}

finally

{

//unexpected error

}

}

Thanks - Sean
Feb 27 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
If you aren't going to use it, then don't reference it

catch (System.FormatException)

--
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.53369$Ug4.7762@dukeread12...
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(object sender, System.EventArgs e)

{

try

{

int intNum = Int32.Parse(txtNumEntry.Text);

long lngNum = Int64.Parse(txtNumEntry.Text);

}

catch (System.FormatException fx) //Non integer input

{

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

MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

}

finally

{

//unexpected error

}

}

Thanks - Sean

Feb 27 '06 #2

P: n/a
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(object sender, System.EventArgs e)

{

try

{

int intNum = Int32.Parse(txtNumEntry.Text);

long lngNum = Int64.Parse(txtNumEntry.Text);

}

catch (System.FormatException fx) //Non integer input

{

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

MessageBoxButtons.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

P: n/a

"Boblemar" <ar*************@logicim.com> wrote in message
news:uM**************@TK2MSFTNGP09.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

P: n/a
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.53385$Ug4.43518@dukeread12...

"Boblemar" <ar*************@logicim.com> wrote in message
news:uM**************@TK2MSFTNGP09.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

P: n/a
Sandeep <Sa*****@mailinator.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.com>
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

P: n/a
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

P: n/a
Bruce Wood <br*******@canada.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.DoNothingWith(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.com>
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 discussion thread is closed

Replies have been disabled for this discussion.