The variable "MyException" is declared, but never used

I need to clear this warning:

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

Complex tells me:

The variable "MyException" is declared, but never used

How can I fix this.

+55
c # exception frameworks ambiguity
Jun 23 2018-11-11T00:
source share
5 answers
  • You can remove it as follows:

     try { doSomething() } catch (AmbiguousMatchException) { doSomethingElse() } 
  • Use the disable warning as follows:

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

Other familiar warnings are disabled

 #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. 
+107
Jun 23 2018-11-11T00:
source share

You declare the name of the exception, MyException, but you never do anything with it. Since it is not used, the compiler points to this.

You can simply delete the name.

 catch(AmbiguousMatchException) { doSomethingElse(); } 
+25
Jun 23 2018-11-11T00:
source share

You can simply write:

 catch (AmbiguousMatchException) 

and omit the name of the exception if you will not use it in the catch clause.

+17
Jun 23 2018-11-11T00:
source share

The problem is that you are not using the MyException variable anywhere. It is declared, but not used. This is not a problem ... just the compiler gives you a hint if you intend to use it.

+2
Jun 23 2018-11-11T00:
source share

You can write an exception to the log if you have one run. It may be useful to identify any problems.

 Log.Write("AmbiguousMatchException: {0}", MyException.Message); 
+2
Jun 23 '11 at 15:04
source share



All Articles