springboot中异常自定义实现

个人博客

在项目中,通常需要自己定义一些异常,以便更好的来管理我们的业务。通常来说,需要有一个我们自己的异常抽象,一个通用的异常类,以及一些特定条件下的异常类。如下所示:

sb-exception

自定义异常抽象

package com.lazycece.sbac.exception.exception;

/**
 * @author lazycece
 * @date 2019/02/23
 */
public abstract class AbstractGlobalException extends RuntimeException {
    public AbstractGlobalException() {
    }

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

    public AbstractGlobalException(String message, Throwable cause) {
        super(message, cause);
    }

    public AbstractGlobalException(Throwable cause) {
        super(cause);
    }

    /**
     * global-custom-exception's code
     *
     * @return int
     */
    abstract public int getCode();
}

自定义通用异常

package com.lazycece.sbac.exception.exception;

/**
 * @author lazycece
 */
public class GlobalException extends AbstractGlobalException {

    private Integer code;

    public GlobalException(String message, Integer code) {
        super(message);
        this.code = code;
    }

    public GlobalException(String message, Throwable cause, Integer code) {
        super(message, cause);
        this.code = code;
    }

    public GlobalException(Throwable cause, Integer code) {
        super(cause);
        this.code = code;
    }

    @Override
    public int getCode() {
        return this.code;
    }
}

特定域异常

特定域方面的异常通常随着业务的情况分为不同的类,这里以在业务层的参数校验的异常为例,代码如下:

package com.lazycece.sbac.exception.exception;


import com.lazycece.sbac.exception.response.ResCode;

/**
 * @author lazycece
 */
public class ParamException extends AbstractGlobalException {
    public ParamException() {
    }

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

    public ParamException(String message, Throwable cause) {
        super(message, cause);
    }

    @Override
    public int getCode() {
        return ResCode.PARAM_ERROR;
    }
}

案例源码

案例源码地址:https://github.com/lazycece/springboot-actual-combat/tree/master/springboot-ac-exception

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot的线程池异步处理通常涉及到使用`ThreadPoolTaskExecutor`或者`AsyncConfigurer`来配置一个后台任务执行器。如果你想要在异步任务执行过程自定义异常处理,你可以这样做: 1. 配置一个自定义的`ThreadPoolTaskExecutor`: ```java @Configuration public class ThreadPoolConfig { @Bean public ThreadPoolTaskExecutor executor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(5); executor.setMaxPoolSize(10); executor.setQueueCapacity(256); // 缓存队列大小 executor.setThreadNamePrefix("my-task-"); executor.initialize(); // 初始化设置 executor.afterPropertiesSet(); // 设置后立即启动 executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 自定义拒绝策略 return executor; } } ``` 在这个例子,我们设置了自定义的`RejectedExecutionHandler`,当线程池饱和无法接受新的任务时,会运行提交任务的代码。 2. 自定义异常处理器: 创建一个实现了`RejectedExecutionHandler`接口的类,并重写`rejectedExecution`方法来捕获并处理异常。 ```java @Component public class CustomRejectedExecutionHandler implements RejectedExecutionHandler { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { try { throw new MyCustomException("Cannot execute task: " + r.toString()); } catch (MyCustomException e) { // 这里可以记录日志、发送通知或做其他异常处理操作 log.error(e.getMessage(), e); } } } ``` 然后,在需要异步执行的任务,你可以在try-catch块抛出`MyCustomException`,这个异常会被`CustomRejectedExecutionHandler`捕获并按照你的意愿处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值