java定义自定义异常,如何在Java中自定义自定义异常?

This is the original exception code

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();

}

}

and this is the new one

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();

}

}

All I need is to change the part of the toString() method so instead of errorMsg.append("[" + super.getMessage() + "]:"); I have errorMsg.append(super.getMessage());. The problem appears when, in a method, the original is thrown because the catch block set to NoValueForParametarWebServiceException doesn't catch the original. I know I could catch the original and just re-throw the new one (which would also be satisfying), but I was wondering if there is another way.

EDIT: It seems what I need is unclear, so to be more clear:

The program throws NoValueForParametarException. I want to catch it but use the toString() method of NoValueForParametarWebServiceException (that is the sole reason of creating the new class) because I need the output format of the new version without changing the old.

解决方案

I don't see any reason to subclass your first exception. Also, if you get rid of the nestedException instance variable and use java.lang.Throwable's cause instead, you don't have to mess with overriding toString and you can delete most of this code.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值