Agent沙箱四堵墙:从Docker到runc的实战隔离指南
2026/10/13 17:11:21
// 一个异常对象至少包含这些信息:ArithmeticExceptione=newArithmeticException("/ by zero");e.getMessage();// "/ by zero" —— 错误描述e.getStackTrace();// 堆栈信息 —— 记录了异常在哪些类的哪些方法里传播过// 比如:at com.lucky.service.LotteryDrawService.doDraw(LotteryDrawService.java:62)// at com.lucky.service.impl.LotteryServiceImpl.draw(LotteryServiceImpl.java:45)// at com.lucky.controller.LotteryController.draw(LotteryController.java:29// 例子1:空指针Stringname=null;name.length();// JVM 自动抛 NullPointerException// 例子2:数组越界int[]arr=newint[3];arr[5]=1;// JVM 自动抛 ArrayIndexOutOfBoundsException// 例子3:类型转换错误Objectobj="hello";Integernum=(Integer)obj;// JVM 自动抛 ClassCastExceptionif(round.getStatus()!=null&&round.getStatus()==1){thrownewBusinessException("该轮次已完成抽奖");// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^// 手动创建 BusinessException 对象,用 throw 抛出// 这行之后的代码不会执行,方法立即"中断"}System.out.println("这行永远不会执行(如果上面抛了异常)");try{// 可能出异常的代码intresult=10/0;}catch(ArithmeticExceptione){// 捕获到异常,在这里处理System.out.println("除数不能为0: "+e.getMessage());}// 异常被 catch 住了,不会继续向上抛,程序继续往下执行// 你可以理解为 Spring 自动帮你做了这件事(伪代码):try{// 执行 Controller 方法Objectresult=lotteryController.draw(roundId);returnresult;}catch(BusinessExceptione){// 匹配到 @ExceptionHandler(BusinessException.class)returnglobalExceptionHandler.handleBusinessException(e);}catch(MethodArgumentNotValidExceptione){returnglobalExceptionHandler.handleValidationException(e);}catch(RuntimeExceptione){returnglobalExceptionHandler.handleRuntimeException(e);}catch(Exceptione){returnglobalExceptionHandler.handleException(e);}@ExceptionHandler(BusinessException.class)publicResult<Void>handleBusinessException(BusinessExceptione){// 第1步:打日志(warn 级别,因为是预期内的业务异常,不是系统崩溃)log.warn("业务异常: {}",e.getMessage());// 控制台输出:业务异常: 该轮次已完成抽奖// 第2步:构造统一返回体returnResult.error(e.getCode(),e.getMessage());// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^// e.getCode() = 500(BusinessException 默认 code)// e.getMessage() = "该轮次已完成抽奖"// 返回 Result{code:500, message:"该轮次已完成抽奖", data:null}}// 第3步:Spring 把 Result 对象序列化成 JSON// 第4步:通过 HTTP 响应返回给前端// 前端收到的 HTTP 响应:// HTTP/1.1 200 OK// Content-Type: application/json// Body: {"code":500,"message":"该轮次已完成抽奖","data":null}@ExceptionHandler(MethodArgumentNotValidException.class)@ResponseStatus(HttpStatus.BAD_REQUEST)// ← HTTP 状态码 400publicResult<Void>handleValidationException(...){...}