I get a warning that I want to get rid of

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hello!

When I compile my program where this method ValidateNumericInput below is
part of I get the following warning
Warning 1 The variable 'ex' is declared but never used
It it possible to get rid of this warning for this ex because I'm not using
it but I must have it because of
checking for FormatException.

private bool ValidateNumericInput(Control control)
{
try
{
int temp = int.Parse(control.Text);
}
catch (FormatException ex)
{
return false;
}
return true;
}

//Tony
 
Tony Johansson said:
When I compile my program where this method ValidateNumericInput below is
part of I get the following warning
Warning 1 The variable 'ex' is declared but never used
It it possible to get rid of this warning for this ex because I'm not
using it but I must have it because of
checking for FormatException.

private bool ValidateNumericInput(Control control)
{
try
{
int temp = int.Parse(control.Text);
}
catch (FormatException ex)
{
return false;
}
return true;
}

If you are not using "ex", just remove it:

....
catch (FormatException)
{
return false;
}
 
Hello Tony,
Hello!

When I compile my program where this method ValidateNumericInput below
is
part of I get the following warning
Warning 1 The variable 'ex' is declared but never used
It it possible to get rid of this warning for this ex because I'm not
using
it but I must have it because of
checking for FormatException.
private bool ValidateNumericInput(Control control)
{
try
{
int temp = int.Parse(control.Text);
}
catch (FormatException ex)
{
return false;
}
return true;
}
//Tony


First of all, the easiest way to remove the error is to write Catch(FormatException)
instead of catch (FormatException ex).

But then you'll be catching an exception which you could have prevented in
the first place.

Instead of Parse, use TryParse:

int tmp = -1;
return int.TryParse(control.Text, CultureInfo.CurrentCulture, out tmp);

I'm writing this out of my own memory, and I'm prettey sure this is the exact
syntax. TryParse will never throw an exception, it will just return false
of it didn't parse in the first place. Should you want to do further checking
on the actual value, tmp will be populated with the value if TryParse succeeds.
 
Tony Johansson said:
Hello!

When I compile my program where this method ValidateNumericInput below is
part of I get the following warning
Warning 1 The variable 'ex' is declared but never used
It it possible to get rid of this warning for this ex because I'm not
using it but I must have it because of
checking for FormatException.

private bool ValidateNumericInput(Control control)
{
try
{
int temp = int.Parse(control.Text);
}
catch (FormatException ex)
{
return false;
}
return true;
}

//Tony

Hi,

If You are not using the variable 'ex' simply remove it (but keep
FormatException for exception filtering).

Hope You find this useful
-Zsolt
 
Tony Johansson said:
Hello!

When I compile my program where this method ValidateNumericInput below is
part of I get the following warning
Warning 1 The variable 'ex' is declared but never used
It it possible to get rid of this warning for this ex because I'm not
using it but I must have it because of
checking for FormatException.

private bool ValidateNumericInput(Control control)
{
try
{
int temp = int.Parse(control.Text);
}
catch (FormatException ex)
{
return false;
}
return true;
}

//Tony

To take this a bit further, ex is an object variable the can hold the
address of a FormatException (or subclass) object, so it gives you a
reference to the thrown exception. The name is used to qualify references to
properties or methods defined for that object type. So if, for example, you
wanted to refer to a FormatException property such as Message, or to a
method such as ToString() in your catch block code, you can. Since you
don't, no need to create the object variable. The exception is still caught
by

catch(FormatException)

You just don't have a reference to it.
 
First of all, the easiest way to remove the error is to write
Catch(FormatException) instead of catch (FormatException ex).

But then you'll be catching an exception which you could have prevented in
the first place.

Instead of Parse, use TryParse:

int tmp = -1;
return int.TryParse(control.Text, CultureInfo.CurrentCulture, out tmp);

I'm writing this out of my own memory, and I'm prettey sure this is the
exact syntax. TryParse will never throw an exception, it will just return
false of it didn't parse in the first place. Should you want to do further
checking on the actual value, tmp will be populated with the value if
TryParse succeeds.

To expand on this, Microsoft recommends using TryParse . Throwing and
catching exceptions has a lot of overhead that can hurt your application
performance.

Andrew Faust
 
Hello Andrew,
To expand on this, Microsoft recommends using TryParse . Throwing and
catching exceptions has a lot of overhead that can hurt your
application performance.

I should have mentioned that... Thanx!
 
Back
Top