suppress 'variable is declared but never used' for exception variables?

  • Thread starter Thread starter Les Caudle
  • Start date Start date
L

Les Caudle

If I have code such as:

catch (WebException WebExcp) {
return false;
}

I will get a warning CS0168: The variable 'WebExcp' is declared but never used

when compiling.

Is there a way to suppress this for individual cases - or failing that,
globally?
 
Les,

You can always change the catch to:

catch (WebException)
{
return false;
}

This way, the variable is not declared, and the error doesn't come up.

Hope this helps.
 
Les said:
If I have code such as:

catch (WebException WebExcp) {
return false;
}

I will get a warning CS0168: The variable 'WebExcp' is declared but never used

when compiling.

Is there a way to suppress this for individual cases - or failing that,
globally?

public class Unreferenced {
[System.Diagnostics.Conditional( "DEBUG")]
static public void Parameter( params object [] o)
{
return;
}
}

// ...

try {
//
}
catch (Exception WebExcp) {
Unreferenced.Parameter( WebExcp);
return;
}
 
mikeb said:
Is there a way to suppress this for individual cases - or failing that,
globally?

public class Unreferenced {
[System.Diagnostics.Conditional( "DEBUG")]
static public void Parameter( params object [] o)
{
return;
}
}

// ...

try {
//
}
catch (Exception WebExcp) {
Unreferenced.Parameter( WebExcp);
return;
}

While this will work, it would be better just to not declare WebExcp at
all:

catch (Exception)
{
....
}
 
Jon said:
While this will work, it would be better just to not declare WebExcp at
all:

catch (Exception)
{
...
}

Normally, yes. However, sometimes I have the exception variable there
so I can examine it when debugging.
 
mikeb said:
Normally, yes. However, sometimes I have the exception variable there
so I can examine it when debugging.

It also has use for suppressing the warning for unreferenced locals or
method parameters.

I know that in a perfect world the need for this wouldn't exist, but
there are times (particularly for debugging and maintenance of code
whose interfaces can't be modified) that this technique can be useful.
 
public class Unreferenced {
[System.Diagnostics.Conditional( "DEBUG")]
static public void Parameter( params object [] o)
{
return;
}
}

// ...

try {
//
}
catch (Exception WebExcp) {
Unreferenced.Parameter( WebExcp);
return;
}

sorry, WHAT?
you catch the exception into an object, pass it thru a class where you dont
handle it ??? - did i miss the superpoint on this, or is this just useless?
 
Am Sat, 19 Jun 2004 05:25:19 +0200 schrieb Crea-Ue. Kirdar:
sorry, WHAT?
you catch the exception into an object, pass it thru a class where you dont
handle it ??? - did i miss the superpoint on this, or is this just useless?

forget it, i saw the answer to jons post too late, sorry. :)
 

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

Back
Top