【发布时间】:2010-08-17 17:21:04
【问题描述】:
这是原始异常代码
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();
}
}
我所需要的只是更改toString() 方法的一部分,所以我有errorMsg.append(super.getMessage()); 而不是errorMsg.append("[" + super.getMessage() + "]:");。当在一个方法中由于设置为NoValueForParametarWebServiceException 的catch 块没有捕捉到原始数据而抛出原始数据时,就会出现问题。我知道我可以抓住原来的,然后重新扔新的(这也很令人满意),但我想知道是否还有其他方法。
编辑: 看来我需要什么不清楚,所以要更清楚:
程序抛出NoValueForParametarException。我想抓住它但使用NoValueForParametarWebServiceException 的toString() 方法(这是创建新类的唯一原因),因为我需要新版本的输出格式而不改变旧版本。
【问题讨论】: