The variable 'MyException' is declared but never used
Asked Answered
K

7

84

I need to clear this warning :

try
{
    doSomething()
}
catch (AmbiguousMatchException MyException)
{
    doSomethingElse()
}

The complier is telling me :

The variable 'MyException' is declared but never used

How can I fix this.

Kumkumagai answered 23/6, 2011 at 14:26 Comment(0)
O
163
  1. You can remove it like this:

    try
    {
        doSomething()
    }
    catch (AmbiguousMatchException)
    {
        doSomethingElse()
    }
    
  2. Use warning disable like this:

    try
    {
        doSomething()
    }
    #pragma warning disable 0168
    catch (AmbiguousMatchException exception)
    #pragma warning restore 0168
    {
        doSomethingElse()
    }
    

Other familiar warning disable

#pragma warning disable 0168 // variable declared but not used.
#pragma warning disable 0219 // variable assigned but not used.
#pragma warning disable 0414 // private field assigned but not used.
Osprey answered 23/6, 2011 at 14:27 Comment(7)
A list of all compiler errors and warnings are available at the MSDNOsprey
The solution given by @Udella is better since not advisable to supress warnings when we can fix itPansophy
@dasariramacharanprasad that was my first suggestion, re-read my answer ;)Osprey
solution #2 is not a solution, it is a mask.Doglike
But it is a sharp one isn't it :)Osprey
#2 is useful when you want to look at the exception in the debugger or use it in debug only code. (Yes, I realize that #if DEBUG blocks introduce a code smell of their own.)Hebraism
yes but this answer is a bit TLDR whilst Khepri & fparadise2 is short and sweet, why even suggest any hacks, you can just ignore all errors and warnings in your head as well :-) I know some devs do that a lot :)Evvoia
E
46

You declare a name for the exception, MyException, but you never do anything with it. Since it's not used, the compiler points it out.

You can simply remove the name.

catch(AmbiguousMatchException)
{
   doSomethingElse();
}
Embitter answered 23/6, 2011 at 14:27 Comment(0)
U
33

You can simply write:

catch (AmbiguousMatchException)

and omit the exception name if you won't be using it in the catch clause.

Udella answered 23/6, 2011 at 14:28 Comment(1)
+1 since it resolves the warning! not explained in MSDN.Pansophy
H
4

You could write the exception out to a log if you've got one running. Might be useful for tracking down any problems.

Log.Write("AmbiguousMatchException: {0}", MyException.Message);
Holguin answered 23/6, 2011 at 15:4 Comment(0)
F
3

The trouble is, you aren't using your variable MyException anywhere. It gets declared, but isn't used. This isn't a problem... just the compiler giving you a hint in case you intended to use it.

Fatso answered 23/6, 2011 at 14:28 Comment(0)
M
2

but never used means that you should use it after catch() such as writing its value to console, then this warning message will disappear.

catch (AmbiguousMatchException MyException)
{
    Console.WriteLine(MyException); // use it here
}
Mccorkle answered 8/10, 2018 at 18:42 Comment(0)
A
1

Just ran into this, where the exception was used based upon compiler variables...

Anyway, my solution was:

 _ = MyException;

Picks it up as used, acknowledges that it isn't really being used.

Adermin answered 10/3, 2023 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.