Unused exception variables causing warnings

A

aioe.cjb.net

So, I was planning on ridding my VS2005 solution of all warnings, but the
ones sounding "The variable 'ex' is declared but never used" in
catch-blocks, are causing a headache.

I have several cases of code like this:

try
{
// Some code
}
catch (Exception ex)
{
LogTool.Log("Error deserializing XML reference tag.");
// I don't care what ex is
}

There is no way i can create a catch block without declaring the ex
variable, right? So what do I do when I want the catch block, don't want to
use the variable, don't want the warning, and don't want to change the
warning level in VS2005? :)

I could possibly do some dummy operation on ex. Any suggestion? (Then
again... What's worse - Having dummy code overhead in the actual application
or warnings clogging the compialtion log.)

Regards,
Frode Nilsen
 
M

Matt

aioe.cjb.net said:
So, I was planning on ridding my VS2005 solution of all warnings, but the
ones sounding "The variable 'ex' is declared but never used" in
catch-blocks, are causing a headache.

I have several cases of code like this:

try
{
// Some code
}
catch (Exception ex)
{
LogTool.Log("Error deserializing XML reference tag.");
// I don't care what ex is
}

There is no way i can create a catch block without declaring the ex
variable, right? So what do I do when I want the catch block, don't want to
use the variable, don't want the warning, and don't want to change the
warning level in VS2005? :)

You don't have to declare a variable:

catch ( Exception )
{
}

Matt
 
A

AlanT

aioe.cjb.net said:
try
{
// Some code
}
catch (Exception ex)
{
LogTool.Log("Error deserializing XML reference tag.");
// I don't care what ex is
}

If you really do not care what type of exception is occuring and the
only action you are performing in the catch block is to log the error
then a possibility is to add 'top level' exception handlers for

Application.ThreadException
AppDomain.CurrentDomain.UnhandledException

and place the LogTool.Log() call there.

You can then omit the try..catch block altogether and let the exception
propagate up to the top level where it can be logged.


hth,
Alan.
 
M

Mark Wilden

aioe.cjb.net said:
Wow.. Can't believe I didn't try that before I posted :=)

You're forgiven because you can't do the equally useful equivalent with
method parameters lists (like we used to do in C++, back in ancient times).

///ark
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Beside the solution posted by the others I would advise you to use The ex,
maybe you do not care now about it, but if you do this in a sufficient large
number of places you may forget what message you posted where. unless you
search the source code.

I would log at the very least EXception.Message & Exception.StackTrace.

An even better solution would be checking the InnerException property.


IMO it will save you problems down the road.
 

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