avoid compiler warnings

  • Thread starter Thread starter Samuel
  • Start date Start date
S

Samuel

Imagine you have the following code:

try
{
...
}
catch (ThreadAbortException eThread)
{
if (WorkStopped != null) WorkStopped(this, EventArgs.Empty)
}

How can I avoid the 'The variable 'eThreadAbort' is declared but never
used.'?
I think this warning is useful in most contexts, but in catch blocks many
many times the caugth object is not used, you only want to know what type of
error occurred. And I don't want the compiler to avoid this warning always,
only in catch blocks.

I could cheat it, doing:

try
{
...
}
catch (ThreadAbortException eThread)
{
#if DEBUG
UseExceptionObject(eThread);
#endif
if (WorkStopped != null) WorkStopped(this, EventArgs.Empty);
}

#if DEBUG
private void UseExceptionObject(Exception e)
{
System.Diagnostics.Debug.WriteLine(e.Message);
}
#endif

But it would be great an option in the compiler to solve this, or maybe the
compiler already has this option?
Another way could be adding an attribute NotUsedAttiribute and assigning it
to the parameter declaration.
Also I could leave the warnings, but with a big program having 'false
warnings' mixed with real warnings and errors is a mess.

Thank you.
 
Just leave off the variable that names the exception (eThread), as in:

try
{
...
}
catch (ThreadAbortException)
{
if (WorkStopped != null) WorkStopped(this, EventArgs.Empty)
}
 
Thank you very much Fred.

Fred Mellender said:
Just leave off the variable that names the exception (eThread), as in:

try
{
...
}
catch (ThreadAbortException)
{
if (WorkStopped != null) WorkStopped(this, EventArgs.Empty)
}

type
 
Back
Top