java项目中自定义业务异常

本文介绍了一个Java Web开发中的秒杀系统异常处理方案,包括自定义异常类、业务逻辑层异常抛出、控制器层异常转换及前端响应处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在java  Web开发过程中会遇到各种的业务异常,如扣款余额不足,重复提交,重复秒杀等,我们在后台java代码中最好的方式是将这种通过异常的方式来捕获,然后逐级上抛,最终在页面上给出提示,在网上看到的一些代码贴出来供大家学习


1.定义异常类

package org.seckill.exception;

/**
 * Created by zhangfx on 2017/7/7.
 */
public class SeckillException extends RuntimeException {
    public SeckillException(String message) {
        super(message);
    }

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

2.在接口的实现类中跑出异常

 @Override
    @Transactional
    public SeckillExecution executeSeckill(long seckillId, long userPhone, String md5) throws SeckillException, RepeatKillException, SeckillCloseException {
       if(md5==null||!md5.equals(getMD5(seckillId))){
           throw new SeckillException("seckill data rewrite");
       }
        //执行秒杀逻辑
        Date nowTime = new Date();
        try{
            //记录购买行为
            int insertCount = successKilledDao.insertSuccessKilled(seckillId,userPhone);
            if(insertCount<=0){
                throw  new RepeatKillException("seckill repeated");
            }else{
                //减库存,热点商品竞争
                int updateCount = seckillDao.reduceNumber(seckillId,nowTime);
                if(updateCount<=0){
                    //没有更新到秒杀记录,秒杀结束,rollback
                    throw new SeckillCloseException("seckill data rewrite");
                }else{
                    //commit
                    SuccessKilled successKilled=successKilledDao.queryByIdWithSeckill(seckillId,userPhone);
                    return new SeckillExecution(seckillId, SeckillStatEnum.SUCCESS,successKilled);
                }
            }
        }catch (SeckillCloseException e1){
            throw e1;
        }catch (RepeatKillException e2){
            throw e2;
        }catch (Exception e){
            logger.error(e.getMessage(), e);
            //所有编译异常转化为运行期异常
            throw new SeckillException("seckill inner error:"+e.getMessage());
        }
    }

3.在controller中接到异常,转为相应的状态码,返回到前台

   @RequestMapping(value = "/{seckillId}/{md5}/execution",
            method = RequestMethod.POST,
            produces={"application/json;charset=UTF-8"})
    @ResponseBody
    public SeckillResult<SeckillExecution> execute(@PathVariable("seckillId") Long seckillId,
                                                   @PathVariable("md5")String md5,
                                                   @CookieValue(value="killPhone",required = false) Long phone){
        if(phone==null){
            return new SeckillResult<SeckillExecution>(false,"未注册");
        }
        SeckillResult<SeckillExecution>  result;
        try{
            //调用代码执行秒杀
            //SeckillExecution  execution= seckillService.executeSeckill(seckillId,phone,md5);
            //调用存储过程执行秒杀
            SeckillExecution  execution= seckillService.executeSeckillProcedure(seckillId,phone,md5);
            return new SeckillResult<SeckillExecution>(true,execution);
        }catch (RepeatKillException e){
            SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.REPEAT_KILL);
            return new SeckillResult<SeckillExecution>(true,execution);
        }catch (SeckillCloseException e){
            SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.END);
            return new SeckillResult<SeckillExecution>(true,execution);
        }catch(Exception e){
            SeckillExecution execution = new SeckillExecution(seckillId, SeckillStatEnum.INNER_ERROR);
            return new SeckillResult<SeckillExecution>(true,execution);
        }

    }

4.界面上接到相应的异常状态码,然后做出相应的提示

 handleSeckillkill : function(seckillId,node){
      //处理秒杀逻辑
        node.hide()
            .html("<button class='btn btn-primary btn-lg' id='killBtn'>开始秒杀</button>");
        $.post(seckill.URL.exposer(seckillId),{},function(result){
            //在回调函数中,执行交互
            if(result && result['success']){
                var exposer = result['data'];
                if(exposer['exposed']){
                    //开启秒杀
                    //获取秒杀地址
                    var md5 = exposer['md5'];
                    var killUrl = seckill.URL.execution(seckillId,md5);
                    console.log("killUrl:"+killUrl);
                    //绑定一次点击事件
                    $('#killBtn').one('click',function(){
                        //绑定秒杀请求
                        //1.先禁用按钮
                        $(this).addClass('disabled');
                        //2.发送秒杀请求
                        $.post(killUrl,{},function(result){
                            if(result && result['success']){
                                var killResult = result['data'];
                                var state = killResult['state'];
                                var stateInfo = killResult['stateinfo'];
                                //显示秒杀结果
                                node.html('<span class="label label-success">'+stateInfo+'</span>');
                            }
                        });
                    });
                    node.show();
                }else{
                    //未开启秒杀
                    var now = exposer['now'];
                    var start = exposer['start'];
                    var end = exposer['end'];
                    //重新计算逻辑
                    seckill.countdown(seckillId,now,start,end);
                }
            }else{
                console.log('result'+result);
            }

        });
    }







### 创建和使用自定义异常Java 中,为了处理特定业务逻辑中的错误情况,通常会创建自定义异常类。这不仅提高了代码的可读性和维护性,还使得程序能够更精确地描述遇到的问题。 #### 定义自定义异常类 继承 `Exception` 或者其子类来创建一个新的异常类是一个常见的做法。通过这种方式可以利用已有的异常层次结构并保持一致性[^1]: ```java public class MyCustomException extends Exception { public MyCustomException(String message) { super(message); } public MyCustomException(String message, Throwable cause) { super(message, cause); } } ``` 当需要抛出自定义异常时,在适当的位置调用构造函数即可传递具体的错误信息给捕获层处理[^2]。 #### 抛出与捕捉自定义异常 为了让应用程序更好地响应这些特殊状况下的失败情形,可以在方法签名处声明可能发生的异常类型,并在其内部逻辑判断合适时机下显式地 throw 对象实例化后的自定义异常对象;而在上层调用方则应该尝试 try-catch 结构来进行相应的恢复操作或记录日志等措施[^3]: ```java // 方法内抛出异常 if (someConditionIsFalse()) { throw new MyCustomException("条件不满足"); } try { // 调用可能会引发异常的方法 } catch (MyCustomException e) { System.err.println(e.getMessage()); // 进行必要的清理工作或其他补偿行为 } ``` #### 使用场景举例说明 假设正在开发一个文件上传服务端接口,如果客户端发送过来的数据不符合预期格式,则可以通过如下方式告知对方具体原因而不只是返回通用 HTTP 错误码[^4]: ```java class InvalidFileFormatException extends RuntimeException { private static final long serialVersionUID = 1L; public InvalidFileFormatException(String msg){ super(msg); } } void validateFileFormat(MultipartFile file) throws InvalidFileFormatException{ if (!file.getContentType().equals("image/jpeg")){ throw new InvalidFileFormatException("仅支持JPEG图片"); } } ``` 上述例子展示了如何针对不同类型的输入验证失败设计专门化的异常类别以便于后续调试分析以及用户体验优化[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值