The variable 'MyException' is declared but never used
Solution 1
You can remove it like this:
try { doSomething() } catch (AmbiguousMatchException) { doSomethingElse() }
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.
Related videos on Youtube
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, 2022Comments
-
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 almost 13 yearsA list of all compiler errors and warnings are available at the MSDN
-
Ram almost 11 yearsThe solution given by @fparadis2 is better since not advisable to supress warnings when we can fix it
-
Jalal Said almost 11 years@dasariramacharanprasad that was my first suggestion, re-read my answer ;)
-
Gusdor about 10 yearssolution #2 is not a solution, it is a mask.
-
Jalal Said about 10 yearsBut it is a sharp one isn't it :)
-
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.)