抛出自定义异常时,如何取消堆栈打印?只提示自定义异常的提示文字即可,不要堆栈信息的那种
你好,针对你的问题,我的答案如下,供参考
比如平时你在业务中写自定义异常的代码如下:
public class BusinessException extends RuntimeException{
private BusinessExceptionCode code;
public BusinessException (BusinessExceptionCode code) {
super(code.getDesc());
this.code = code;
}
public BusinessExceptionCode getCode() {
return code;
}
public void setCode(BusinessExceptionCode code) {
this.code = code;
}
/**
* 不写入堆栈信息,提高性能
*/
@Override
public Throwable fillInStackTrace() {
return this;
}
}
在其中,你只需要重写fillInStackTrace方法即可
使用全局异常处理,针对自定义异常只输出信息即可