try catch - "Variable declared but never used" ?

W

What-A-Tool

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
 
R

Robbe Morris [C# MVP]

If you aren't going to use it, then don't reference it

catch (System.FormatException)
 
B

Boblemar

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
 
W

What-A-Tool

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
 
J

Jon Skeet [C# MVP]

Sandeep said:
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.
 
B

Bruce Wood

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

Jon Skeet [C# MVP]

Bruce Wood said:
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...
 

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