Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress warning on unused exception variable in C#

I have this code:

try {     someMethod(); } catch (XYZException e) {     // do something without using e } 

Doing this will give me a warning about declaring but never using e, which I hate. However, I also don't want to use a catch clause without that variable, because then it will catch all exceptions, not just XYZExceptions. This seems like a fairly often occurring pattern. I know I can use #pragma warning disable 0168 to suppress the warning, but I don't really find that a very elegant solution. Is there a better way?

like image 973
Jordi Avatar asked Dec 21 '10 14:12

Jordi


People also ask

How do I get rid of the unused variable warning?

Solution: If variable <variable_name> or function <function_name> is not used, it can be removed. If it is only used sometimes, you can use __attribute__((unused)) . This attribute suppresses these warnings.

What does unused variable mean in C?

No nothing is wrong the compiler just warns you that you declared a variable and you are not using it. It is just a warning not an error. While nothing is wrong, You must avoid declaring variables that you do not need because they just occupy memory and add to the overhead when they are not needed in the first place.

What is unused parameter?

Reports the parameters that are considered unused in the following cases: The parameter is passed by value, and the value is not used anywhere or is overwritten immediately. The parameter is passed by reference, and the reference is not used anywhere or is overwritten immediately.


1 Answers

Define the catch clause without the exception variable as follows:

try {     someMethod(); } catch (XYZException) {     // do something without using e } 
like image 104
Jan Avatar answered Sep 18 '22 04:09

Jan