自定义异常的性能问题:避免打印繁重的“栈”信息

Java每实例化一个Exception,就会对当时的栈进行快照,如果该动作发生的比较频繁,对内存的开销也就不可忽略了!

常见的自定义异常格式:

public class ParamException extends RuntimeException{

    public ParamException() {
        super();
    }
    public ParamException(String message) {
        super(message);
    }
}

抛出业务异常段代码:

    @GetMapping("/valid")
    public Object test(String param){
       if (param == null){
           throw new ParamException("参数为空");
       }
       return Res.success();
    }

打印出来的业务异常信息:

2020-12-21 14:54:38.328 ERROR com.hrbank.aspect.ExceptionAdvice >> system.exception.occur
com.hrbank.exception.ParamException: 参数为空
	at com.hrbank.controller.ManageAdvertiseController.test(ManageAdvertiseController.java:63)
	at com.hrbank.controller.ManageAdvertiseController$$FastClassBySpringCGLIB$$6b367347.invoke()
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:769)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747)
	at org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:120)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.p

你可能感兴趣的:(并发编程,java)