2013-01-19 3 views
2

컴파일러 (주석 또는 기타)에서 Java 함수가 결코 반환되지 않는 (즉, 항상 throw되는) 것을 깨닫게하는 방법이 없으므로 마지막 문장으로 그 사용법을 오류없이 출력합니다 무효를 반환하는 다른 함수에서? java 함수 주석을 반환하지 않습니다.

int add(int x, int y) { 
    throwNotImplemented(); // compiler error here: no return value. 
} 

// How can I annotate (or change) this function, so compiling add will not yield an error since this function always throws? 
void throwNotImplemented() { 
    ... some stuff here (generally logging, sometimes recovery, etc) 
    throw new NotImplementedException(); 
} 

감사합니다 :

여기 단순화/만들어 낸 예입니다.

+1

그것은 분명하지 않다. 너 무슨 뜻이야? – Swapnil

+0

나는 무엇이 문제인지 이해할 수 없다. 무엇을하려고 하는가? – Maroun

+0

나는 addTop 함수를 그대로 컴파일 할 수 있지만 오류없이 컴파일 할 수 있도록 throwNotImplemented에 변경 사항을 적용해야한다. – daniel

답변

3

아니요, 불가능합니다.

주, 그러나, 당신은 쉽게 해결할 수 있음을 다음과 같이

int add(int x, int y) { 
    throw notImplemented(); 
} 

Exception notImplemented() { 
    ... some stuff here (generally logging, sometimes recovery, etc) 
    return new NotImplementedException(); 
} 
0

구현되지 않은 메소드에서 직접 throw하지 않는 이유는 무엇입니까?

int add(int x, int y) { 
    throw new UnsupportedOperationException("Not yet implemented"); 
} 

방법은 int를 반환하지 않는 경우에도이 잘 컴파일됩니다. 그리고 그것은 그러한 상황에서 사용될 것을 의미하는 standard JDK exception을 사용합니다.

+0

답변 해 주셔서 감사합니다, @assylias. throwNotImplemented 메소드는 추가 작업 (로깅, 복구 등)을 수행해야합니다. 즉,'add'를 던지면 충분하지 않습니다. 이러한 액션을'add' 메소드에 추가하면 중복이 발생합니다 (즉,'multiply' 메소드는 그것들을 가질 필요가 있습니다). – daniel

관련 문제