473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Unused exception variables causing warnings

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
Jun 22 '06 #1
6 5136

aioe.cjb.net wrote:
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

Jun 22 '06 #2
You don't have to declare a variable:

catch ( Exception )
{
}

Matt


Wow.. Can't believe I didn't try that before I posted :=)

Thanks!

Frode
Jun 22 '06 #3
Not only that, but you can take it to the logical extreme with:

try
{
// blah blah
}
catch
{
// Yippee!
}
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"aioe.cjb.net" wrote:
You don't have to declare a variable:

catch ( Exception )
{
}

Matt


Wow.. Can't believe I didn't try that before I posted :=)

Thanks!

Frode

Jun 22 '06 #4

aioe.cjb.net wrote:
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.

Jun 22 '06 #5
"aioe.cjb.net" <fr**********@software-innovation.com> wrote in message
news:e7**********@nntp.aioe.org...
You don't have to declare a variable:

catch ( Exception )
{
}

Matt


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
Jun 22 '06 #6
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.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"aioe.cjb.net" <fr**********@software-innovation.com> wrote in message
news:e7**********@nntp.aioe.org...
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

Jun 22 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: ralphNOSPAM | last post by:
Is there a PHP script that can find unused variables? I'd like to 'clean up' my scripts. Thanks...
11
by: Michael B Allen | last post by:
Is there a standard method for supressing warnings regarding unused parameters? I have a function that might be called hundreds of thousands of times that looks like this: const void *...
3
by: Tom McL. | last post by:
Is there a way I identify and remove unused variable within vb2005 standard edition
12
by: zacks | last post by:
Suddenly, in a VB2005 project I am working on, several variables show up in the list of warnings are being unused local variables. But they all are. Several of them are the ex variable used in a...
11
by: Charles Sullivan | last post by:
I have a number of functions, e.g.: int funct1( int arg1, int arg2, int arg3 ); int funct2( int arg1, int arg2, int arg3 ); int funct3( int arg1, int arg2, int arg3 ); that are called via...
8
by: Frank Rizzo | last post by:
Is there a setting in VS2005 to quickly locate methods that are unused (maybe through compiler warnings)? If not, any utilities out there that do that? Thanks
9
by: werasm | last post by:
Hi all, I'm looking for a nice clean (portable) way to get rid of unused variable warnings without fiddling with compiler settings (on a per case basis). I've come up with this: template...
33
by: jacob navia | last post by:
Hi I am considering extending the lcc-win32 compiler to accept int fn(int a, int b,double) { // body of the function } This allows the programmer to specify that the third parameter is not...
13
by: Rex Mottram | last post by:
I'm using an API which does a lot of callbacks. In classic callback style, each routine provides a void * pointer to carry user-defined data. Sometimes, however, the user-defined pointer is not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.