The variable 'MyException' is declared but never used

59,159

Solution 1

  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.

Solution 2

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();
}

Solution 3

You can simply write:

catch (AmbiguousMatchException)

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

Solution 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);

Solution 5

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.

Share:
59,159

Related videos on Youtube

Wassim AZIRAR
Author by

Wassim AZIRAR

https://www.malt.fr/profile/wassimazirar I am not looking for permanent contract (no need to contact me for that) I develop 💻 in .NET Core, JavaScript, Angular and React ⚛

Updated on July 05, 2022

Comments

  • Wassim AZIRAR
    Wassim AZIRAR almost 2 years

    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.

  • Jalal Said
    Jalal Said almost 13 years
    A list of all compiler errors and warnings are available at the MSDN
  • Ram
    Ram almost 11 years
    The solution given by @fparadis2 is better since not advisable to supress warnings when we can fix it
  • Jalal Said
    Jalal Said almost 11 years
    @dasariramacharanprasad that was my first suggestion, re-read my answer ;)
  • Gusdor
    Gusdor about 10 years
    solution #2 is not a solution, it is a mask.
  • Jalal Said
    Jalal Said about 10 years
    But it is a sharp one isn't it :)
  • Eric Hirst
    Eric Hirst almost 3 years
    #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.)