avoid compiler warnings

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

Fred Mellender

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

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

Samuel

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
 

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