长丰县住房和建设局网站样本之家登录网站
2026/1/14 17:27:19 网站建设 项目流程
长丰县住房和建设局网站,样本之家登录网站,网页加速器pc,天津百度百科1. 基本覆盖范围 try {// 可能抛出各种异常 } catch (Exception e) {// 可以捕获所有继承自Exception的异常// 包括运行时异常和受检异常 }2. 异常继承体系 Throwable (可抛出)├── Error (错误) // catch(Exception) ❌ 不能捕获│ ├── VirtualMach…1.基本覆盖范围try{// 可能抛出各种异常}catch(Exceptione){// 可以捕获所有继承自Exception的异常// 包括运行时异常和受检异常}2.异常继承体系Throwable (可抛出) ├── Error (错误) // catch(Exception) ❌ 不能捕获 │ ├── VirtualMachineError │ ├── OutOfMemoryError │ └── StackOverflowError │ └── Exception (异常) ├── RuntimeException // ✅ 可以捕获 │ ├── NullPointerException │ ├── IllegalArgumentException │ └── ArithmeticException │ └── 其他受检异常 // ✅ 可以捕获 ├── IOException ├── SQLException └── 自定义异常3.重要限制不能捕获Errortry{// 触发堆栈溢出错误recursiveMethod(0);}catch(Exceptione){// 这里不会执行因为StackOverflowError是Error不是ExceptionSystem.out.println(捕获到异常);}catch(Errore){// 需要这样捕获ErrorSystem.out.println(捕获到错误: e);}privatevoidrecursiveMethod(intdepth){if(depth10000)return;// 应该有退出条件但这里故意写错recursiveMethod(depth1);}4.捕获所有Throwable如果要捕获所有异常和错误try{// 可能抛出任何Throwable}catch(Throwablet){// 可以捕获Exception和Errorif(tinstanceofError){// 处理严重错误log.error(发生严重错误,t);}elseif(tinstanceofException){// 处理普通异常log.error(发生异常,t);}}5.Spring Boot中的实际应用场景1全局异常处理RestControllerAdvicepublicclassGlobalExceptionHandler{// 只能处理Exception及其子类ExceptionHandler(Exception.class)publicResponseEntityErrorResponsehandleAllExceptions(Exceptionex){returnResponseEntity.status(500).body(newErrorResponse(系统异常));}// 如果需要处理Error需要单独定义ExceptionHandler(Error.class)publicResponseEntityErrorResponsehandleErrors(Errorerror){// 通常记录日志后让应用关闭log.fatal(系统发生严重错误,error);returnResponseEntity.status(500).body(newErrorResponse(系统错误));}}场景2Service层异常捕获ServicepublicclassUserService{publicUserprocessUser(Stringdata){try{// 业务逻辑returnparseAndSave(data);}catch(Exceptione){// 这里能捕获所有Exception// 但无法捕获VirtualMachineError等// 转换为业务异常thrownewBusinessException(处理用户失败,e);}}// 更完整的版本publicvoidcriticalOperation(){try{// 关键操作performOperation();}catch(Throwablet){// 捕获所有Throwableif(tinstanceofOutOfMemoryError){// 内存不足尝试清理System.gc();thrownewSystemException(内存不足请重试);}elseif(tinstanceofException){// 普通异常处理thrownewBusinessException(操作失败,t);}else{// 其他Error重新抛出throwt;}}}}6.Thread.UncaughtExceptionHandler对于未捕获的异常包括ErrorpublicclassGlobalExceptionHandler{publicstaticvoidsetup(){// 设置默认的未捕获异常处理器Thread.setDefaultUncaughtExceptionHandler((thread,throwable)-{// 这里能捕获所有未处理的Throwableif(throwableinstanceofError){log.fatal(线程 {} 发生严重错误,thread.getName(),throwable);// 可能需要重启应用}else{log.error(线程 {} 抛出未捕获异常,thread.getName(),throwable);}});}}7.最佳实践建议❌不推荐的做法try{// 所有代码}catch(Exceptione){// 什么也不做或只是打印e.printStackTrace();// 生产环境无效}✅推荐的做法ComponentpublicclassSafeExecutor{// 处理可恢复的异常publicTOptionalTexecuteSafely(SupplierTtask){try{returnOptional.ofNullable(task.get());}catch(RuntimeExceptione){// 业务异常记录并返回空log.warn(业务操作失败,e);returnOptional.empty();}catch(Exceptione){// 系统异常需要关注log.error(系统异常,e);returnOptional.empty();}// 不捕获Error让上层处理}// 处理需要区分异常的场合publicvoidprocessWithRetry(Runnabletask,intmaxRetries){intattempts0;while(attemptsmaxRetries){try{task.run();return;}catch(BusinessExceptione){// 业务异常不重试throwe;}catch(TemporaryExceptione){// 临时异常重试attempts;if(attemptsmaxRetries)throwe;waitForRetry(attempts);}catch(Exceptione){// 其他异常包装后抛出thrownewSystemException(操作失败,e);}}}}8.重要总结捕获类型能捕获的异常不能捕获的异常使用场景catch (Exception e)所有Exception子类Error及其子类日常业务异常处理catch (Throwable t)Exception和Error无全部可捕获框架底层、关键组件catch (RuntimeException e)运行时异常受检异常、Error快速失败场景关键点Exception能捕获所有异常但无法捕获ErrorError通常表示严重系统问题不应随意捕获生产环境中应该分层处理异常不要用空的catch块吞掉异常考虑使用Throwable的场景线程池、自定义类加载器等在Spring Boot项目中通常建议Service层抛出业务异常或记录后重新抛出Controller层使用ExceptionHandler处理全局使用ControllerAdvice统一处理ExceptionError由专门的监控系统处理

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询