2010-08-17 2 views
1

이 원래 예외 코드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가 발생합니다. 나는 그것을 잡으려고하지만 NoValueForParametarWebServiceExceptiontoString() 메소드를 사용합니다 (이는 새 클래스를 만드는 유일한 이유입니다). 이전 버전을 변경하지 않고 새 버전의 출력 형식이 필요하기 때문입니다.

답변

1

첫 번째 예외를 서브 클래 싱 할 이유가 없습니다. 또한 nestedException 인스턴스 변수를 제거하고 대신 java.lang.Throwable의 원인을 사용하면 toString을 재정 의하여 처리 할 필요가 없으며이 코드의 대부분을 삭제할 수 있습니다.

+0

논리적 인 것처럼 보이기 때문에 서브 클래 싱을합니다 (사소한 toString 변경과 거의 동일한 동작이 필요합니다). 원래 클래스를 변경하지 않은 이유는 공유 클래스이고 누군가 출력 형식을 필요로하기 때문입니다. – Andrija

0

문제는 catch 블록은 'NoValueForParametarWebServiceException' 원본을 잡으려고하지 않는 으로 설정하기 때문에 방법, 원본이 발생 될 때 나타납니다. 나는 (도 만족스러운 것) 원본과 단지 다시 던져 새로운 당신은 던져 아이 예외를 다시 할 필요가 없습니다

를 잡을 수 있었다 알고있다. 이 경우에는 부모 예외 만 잡습니다. 귀하의 예외 중 하나가 (NoValueForParametarException 또는 NoValueForParametarWebServiceException) 발생하는 경우 위의 경우 catch 블록에서

try{ 
    //your code 
}catch(NoValueForParametarException e){ 
    //whatever handling you need to do. suppose you call toString() method 
    System.out.println(e.toString()); 
} 

가 실행됩니다.

어떤 예외가 발생했는지에 따라 toString() 메서드가 호출됩니다. (단순 상속 규칙) 즉 NoValueForParametarException이 NoValueForParametarException 클래스 에 정의 던져 toString는 예를 들어 불려갑니다

  • .
  • 그리고 NoValueForParametarWebServiceException 이 NoValueForParametarWebServiceException 에서 다음 오버라이드 (override) toString 방법에 던져진다면 호출됩니다.예외와 관련된

일부 자습서 :이 도움이

http://download.oracle.com/javase/tutorial/essential/exceptions/index.html

http://tutorials.jenkov.com/java-exception-handling/index.html

희망.

+0

웹 서비스 호출자에게 toString() 메서드의 결과를 반환하고 다른 형식 ( )에 있어야하기 때문에 항상 NoValueForParametarWebServiceException toString() 메서드가 필요합니다. NoValueForParametarException nvfpe) { \t \t \t \t 결과 = nvfpe.toString(); \t \t \t은}) NoValueForParametarWebServiceException는 toString을 (throw되는 경우 @Andrija)는 이해 NoValueForParametarException – Andrija

+0

나는 문제를 더 명확하게하기 위해 게시하고 편집 할 것이다. – YoK

+0

을 잡을 경우에도 호출 될 것이다, 그러나 그것은 매우 것은 아니다 – Andrija

관련 문제