developer tip

'MyException'변수가 선언되었지만 사용되지 않았습니다.

optionbox 2020. 11. 16. 08:08
반응형

'MyException'변수가 선언되었지만 사용되지 않았습니다.


이 경고를 지워야합니다.

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

컴파일러는 다음과 같이 말합니다.

'MyException'변수가 선언되었지만 사용되지 않았습니다.

이 문제를 어떻게 해결할 수 있습니까?


  1. 다음과 같이 제거 할 수 있습니다.

    try
    {
        doSomething()
    }
    catch (AmbiguousMatchException)
    {
        doSomethingElse()
    }
    
  2. 다음과 같이 경고 비활성화를 사용하십시오.

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

기타 익숙한 경고 비활성화

#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.

예외의 이름 인 MyException을 선언하지만 그에 대해 아무것도하지 않습니다. 사용되지 않기 때문에 컴파일러가 지적합니다.

이름을 간단히 제거 할 수 있습니다.

catch(AmbiguousMatchException)
{
   doSomethingElse();
}

다음과 같이 간단히 작성할 수 있습니다.

catch (AmbiguousMatchException)

catch 절에서 사용하지 않을 경우 예외 이름을 생략하십시오.


실행중인 경우 예외를 로그에 기록 할 수 있습니다. 문제를 추적하는 데 유용 할 수 있습니다.

Log.Write("AmbiguousMatchException: {0}", MyException.Message);

문제는 MyException어디에서나 변수를 사용하지 않는다는 것 입니다. 선언되지만 사용되지 않습니다. 이것은 문제가되지 않습니다. 단지 컴파일러가 사용하려는 경우에 힌트를 제공합니다.


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
}

참고URL : https://stackoverflow.com/questions/6455684/the-variable-myexception-is-declared-but-never-used

반응형