java 自定义的异常类_java自定义异常类实例讲解

才开通博客没几天,不知道写什么东西,就把最近在项目中做的东西分享一下,开通博客也可以监督一下自己,技术的路很遥远,希望能通过写博客来让自己走的更远。

一般在web项目中都需要定义一个全局异常来处理一些业务方面的异常,下面是自定义异常的一些代码。

1.自定义一个异常,然后继承RuntimeException

/**

* @author zhuwenwen

* @date 2018/8/16 20:18

*/

@Getter

public class CustomException extends RuntimeException {

/**

* 异常编码

*/

private Integer code;

/**

* 附加数据

*/

private Object data;

public CustomException(String errorMsg) {

super(errorMsg);

this.code= CustomEnum.ERROR.getCode();

}

public CustomException(String errorMsg, Integer code) {

super(errorMsg);

this.code = code;

}

public CustomException(Integer code, String errorMsg, Throwable errorCourse) {

super(errorMsg,errorCourse);

this.code = code;

}

public CustomException(String message, Integer code, Object data) {

super(message);

this.code = code;

this.data = data;

}

}

2.定义一个全局异常处理的类

/**

* 处理全局异常

*

* @author zhuwenwen

* @date 2018/8/17 9:31

*/

@RestControllerAdvice

public class CustomExceptionHandler extends ResponseEntityExceptionHandler implements CustomExceptionHanlerTrit {

private Logger logger=LoggerFactory.getLogger(CustomExceptionHandler.class);

/**

*

* @see ResponseEntityExceptionHandler#handleExceptionInternal

*/

@Override

protected ResponseEntity handleExceptionInternal(Exception ex, Object body, HttpHeaders headers, HttpStatus status,WebRequest webRequest) {

logger.warn("{}", ex.getMessage());

return handleException(CustomEnum.FAILURE_HTTP.getCode() + status.value(), ex.getMessage(), null);

}

/**

* 处理ServletException

*

* @param ex 异常

* @return 异常处理结果

*/

@ExceptionHandler(value = {ServletException.class})

protected final ResponseEntity handleServletException(ServletException ex) {

logger.error("{}", ex);

return handleException(CustomEnum.FAILURE_SERVLET.getCode(), ex.getMessage(), null);

}

/**

* 处理SQLException

*

* @param ex 异常

* @return 异常处理结果

*/

@ExceptionHandler(value = {SQLException.class})

protected final ResponseEntity handleSQLException(SQLException ex) {

logger.error("{}", ex);

return handleException(CustomEnum.FAILURE_DB.getCode(), ex.getMessage(), null);

}

/**

* 处理ConstraintViolationException

*

* @param ex 异常

* @return 异常处理结果

*/

@ExceptionHandler(value = {ConstraintViolationException.class})

protected final ResponseEntity handleConstraintViolationException(ConstraintViolationException ex) {

logger.error("{}", ex);

String message = ex.getMessage();

if (ex.getConstraintViolations() != null && !ex.getConstraintViolations().isEmpty()) {

message = ex.getConstraintViolations().stream().findFirst().isPresent()

? ex.getConstraintViolations().stream().findFirst().get().getMessage() : null;

}

return handleException(CustomEnum.FAILURE_VALIDATION.getCode(), message, null);

}

/**

* 处理MorphedException

*

* @param ex 异常

* @return 异常处理结果

*/

@ExceptionHandler(value = {CustomException.class})

protected final ResponseEntity handleMorphedException(CustomException ex) {

logger.info("{}", ex);

return handleException(ex.getCode(), ex.getMessage(),ex.getData());

}

/**

* 处理RuntimeException

*

* @param ex 异常

* @return 异常处理结果

*/

@ExceptionHandler(value = {RuntimeException.class})

protected final ResponseEntity handleRuntimeException(RuntimeException ex) {

logger.error("{}", ex);

return handleException(CustomEnum.FAILURE_UNKNOWN.getCode(), ex.getMessage(), null);

}

}

3.封装成一个组件,可以打成jar包,供多个项目使用,那么可以自定义一个注解,然后在启动类上加上这个注解,即可使用。下面是一个简单的demo

/**

* @author zhuwenwen

* @date 2018/8/17 11:02

*/

@Target(ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)

@Import(CustomExceptionHandler.class)

@Documented

public @interface EnableCustomException {

/**

* 是否开启

* @return

*/

boolean enabled() default true;

}

/**

* @author zhuwenwen

* @date 2018/8/17 14:47

*/

public class CustomImportBeanDefination implements ImportBeanDefinitionRegistrar {

@Override

public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {

//是否含有@EnableCustomException注解

if (annotationMetadata.isAnnotated(EnableCustomException.class.getName())){

//获取该注解上面的所有属性,然后封装成一个map

MultiValueMap attributes = annotationMetadata.getAllAnnotationAttributes(EnableCustomException.class.getName());

if(attributes.get(EnableCustomExceptionCode.ENABLED).equals(Boolean.TRUE)){

BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(CustomExceptionHandler.class);

beanDefinitionRegistry.registerBeanDefinition(CustomExceptionHandler.class.getName(),beanDefinitionBuilder.getBeanDefinition());

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值