이 원래 예외 코드Java에서 사용자 정의 예외를 사용자 정의하려면 어떻게해야합니까?
public class NoValueForParametarException extends Exception {
private Exception nestedException;
private int errorCode;
public NoValueForParametarException(String message) {
super(message);
}
public NoValueForParametarException(Exception ex,String message) {
super(message);
this.nestedException = ex;
}
public NoValueForParametarException(String message, int errorCode) {
super(message);
this.setErrorCode(errorCode);
}
public Exception getNestedException() {
return this.nestedException;
}
public void setNestedException(Exception nestedException) {
this.nestedException = nestedException;
}
public int getErrorCode() {
return this.errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public String toString() {
StringBuffer errorMsg = new StringBuffer();
errorMsg.append("[" + super.getMessage() + "]:");
errorMsg.append((this.nestedException != null) ? ("\n[Nested exception]:" + this.nestedException):"");
return errorMsg.toString();
}
}
이며,이 새로운 하나
public class NoValueForParametarWebServiceException extends NoValueForParametarException {
public NoValueForParametarWebServiceException(String message) {
super(message);
}
public NoValueForParametarWebServiceException(Exception ex,String message) {
super(message);
this.setNestedException(ex);
}
public NoValueForParametarWebServiceException(String message, int errorCode) {
super(message);
this.setErrorCode(errorCode);
}
public String toString() {
StringBuffer errorMsg = new StringBuffer();
errorMsg.append(super.getMessage());
errorMsg.append((this.getNestedException() != null) ? ("\n[Nested exception]:" + this.getNestedException()):"");
return errorMsg.toString();
}
}
내가 필요로하는 모든 내가 errorMsg.append(super.getMessage());
을하여 toString()
방법의 일부 그래서 대신 errorMsg.append("[" + super.getMessage() + "]:");
을 변경하는 것입니다. NoValueForParametarWebServiceException
으로 설정된 catch 블록이 원본을 catch하지 않기 때문에 원본 매크로가 throw되는 경우 문제가 발생합니다. 나는 내가 원래 것을 잡을 수 있고 새로운 것을 다시 던질 수 있다는 것을 안다. (그것은 또한 만족 스러울 것이다.) 그러나 나는 다른 방법이 있는지 궁금해하고 있었다.
편집 : 그것은 내가 지금 더 명확하게하기 위해, 불분명 필요한 것 : 을이 프로그램은 NoValueForParametarException
가 발생합니다. 나는 그것을 잡으려고하지만 NoValueForParametarWebServiceException
의 toString()
메소드를 사용합니다 (이는 새 클래스를 만드는 유일한 이유입니다). 이전 버전을 변경하지 않고 새 버전의 출력 형식이 필요하기 때문입니다.
논리적 인 것처럼 보이기 때문에 서브 클래 싱을합니다 (사소한 toString 변경과 거의 동일한 동작이 필요합니다). 원래 클래스를 변경하지 않은 이유는 공유 클래스이고 누군가 출력 형식을 필요로하기 때문입니다. – Andrija