当前位置: 首页 > news >正文

建设初级中学网站淘宝网官方网站

建设初级中学网站,淘宝网官方网站,快速网站仿制,购物网站的排版文章目录 Mabatis处理异常屏蔽SQL返回前端全局异常捕获处理结论1 java异常体系2 Spring框架异常处理3 定位Spring框架转化为哪种unchecked异常3.1 捕获RuntimeException定位Spring框架转化抛出的异常类3.2 进一步查看包名判断3.3 识别MyBatisSystemException下级实现3.3 识别My…

文章目录

  • Mabatis处理异常屏蔽SQL返回前端全局异常捕获处理
    • 结论
    • 1 java异常体系
    • 2 Spring框架异常处理
    • 3 定位Spring框架转化为哪种unchecked异常
      • 3.1 捕获RuntimeException定位Spring框架转化抛出的异常类
      • 3.2 进一步查看包名判断
      • 3.3 识别MyBatisSystemException下级实现
      • 3.3 识别MyBatisSystemException继承实现

Mabatis处理异常屏蔽SQL返回前端全局异常捕获处理

结论

在全局异常处理类中添加MyBatisSystemException即可单独对MyBatis中和数据库操作相关异常操作进行全局处理,同时屏蔽sql内容,只返回文字 “服务错误,请联系系统管理员” 给前端。

@Slf4j
@ControllerAdvice
public class ExceptionHandlerAdvice {/*** Sql查询失败在spring的包装下会统一抛出非受检异常,单独捕获,防止sql语句被返回给前端*/@ResponseBody@ExceptionHandler(MyBatisSystemException.class)public Object handleBindException(HttpServletRequest req, MyBatisSystemException e) {String path = "http://"+req.getRemoteAddr()+":"+req.getServerPort() + req.getRequestURI();log.error("访问 "+path +"报错,报错信息为: "+ e.getMessage(), e);return new BaseResult<>(CodeEnum.E500, false, "服务错误,请联系系统管理员。");}//拦截所有Exception,展示Error页面@ResponseBody@ExceptionHandler({Exception.class})public BaseResult errorHandler(HttpServletRequest req, Exception e) {String path = "http://"+req.getRemoteAddr()+":"+req.getServerPort() + req.getRequestURI();log.error("访问 "+path +"报错,报错信息为: "+ e.getMessage(), e);return new BaseResult<>(CodeEnum.E500, false, e.getMessage());}
}

1 java异常体系

在这里插入图片描述

1.Throwable

所有的异常都是Throwable的直接或者间接子类。Throwable有两个直接子类,Error和Exception。

2.Error

Error是错误,对于所有的编译时期的错误以及系统错误都是通过Error抛出的。这些错误表示故障发生于虚拟机自身、或者发生在虚拟机试图执行应用时,如Java虚拟机运行错误(Virtual MachineError)、类定义错误(NoClassDefFoundError)等。这些错误是不可查的,因为它们在应用程序的控制和处理能力之 外,而且绝大多数是程序运行时不允许出现的状况。对于设计合理的应用程序来说,即使确实发生了错误,本质上也不应该试图去处理它所引起的异常状况。在 Java中,错误通过Error的子类描述。

3.Exception

它规定的异常是程序本身可以处理的异常。异常和错误的区别是,异常是可以被处理的,而错误是没法处理的。

4.Checked Exception【受检异常】

可检查的异常,这是编码时非常常用的,所有checked exception都是需要在代码中处理的。它们的发生是可以预测的,正常的一种情况,可以合理的处理。例如IOException。

5.Unchecked Exception【非受检异常】

RuntimeException及其子类都是unchecked exception。比如NPE空指针异常,除数为0的算数异常ArithmeticException等等,这种异常是运行时发生,无法预先捕捉处理的。Error也是unchecked exception,也是无法预先处理的。

参考:https://juejin.cn/post/6965407291260534820

2 Spring框架异常处理

Spring 提供方便的 API 把具体技术相关的异常(比如由JDBOHibernate or JDO 抛出的)转化为一致的 unchecked 异常。

3 定位Spring框架转化为哪种unchecked异常

3.1 捕获RuntimeException定位Spring框架转化抛出的异常类

直接在ExceptionHandlerAdvice中捕获RuntimeException,然后DEBUG,查看异常class类型,发现都是继承自MyBatisSystemException

在这里插入图片描述

3.2 进一步查看包名判断

进一步查看包名发现为org.springframework.dao,基本可以判定捕获MyBatisSystemException可以实现要求

package org.mybatis.spring;import org.springframework.dao.UncategorizedDataAccessException;public class MyBatisSystemException extends UncategorizedDataAccessException {private static final long serialVersionUID = -5284728621670758939L;public MyBatisSystemException(Throwable cause) {super((String)null, cause);}
}

3.3 识别MyBatisSystemException下级实现

MyBatisSystemException目前没有下级实现类

3.3 识别MyBatisSystemException继承实现

可以看到继承父类均为abstract修饰,一直到NestedRuntimeException继承RuntimeException。则已经找到MyBatisSystemException的所有上级继承父类,进一步确认MyBatisSystemException符合作为全局异常捕获ExceptionHandler的最上级实现异常类型,而不会漏异常捕获。

package org.springframework.dao;import org.springframework.lang.Nullable;public abstract class UncategorizedDataAccessException extends NonTransientDataAccessException {public UncategorizedDataAccessException(@Nullable String msg, @Nullable Throwable cause) {super(msg, cause);}
}
package org.springframework.dao;import org.springframework.lang.Nullable;public abstract class NonTransientDataAccessException extends DataAccessException {public NonTransientDataAccessException(String msg) {super(msg);}public NonTransientDataAccessException(@Nullable String msg, @Nullable Throwable cause) {super(msg, cause);}
}
package org.springframework.dao;import org.springframework.core.NestedRuntimeException;
import org.springframework.lang.Nullable;public abstract class DataAccessException extends NestedRuntimeException {public DataAccessException(String msg) {super(msg);}public DataAccessException(@Nullable String msg, @Nullable Throwable cause) {super(msg, cause);}
}
package org.springframework.core;import org.springframework.lang.Nullable;public abstract class NestedRuntimeException extends RuntimeException {private static final long serialVersionUID = 5439915454935047936L;public NestedRuntimeException(String msg) {super(msg);}public NestedRuntimeException(@Nullable String msg, @Nullable Throwable cause) {super(msg, cause);}@Nullablepublic String getMessage() {return NestedExceptionUtils.buildMessage(super.getMessage(), this.getCause());}@Nullablepublic Throwable getRootCause() {return NestedExceptionUtils.getRootCause(this);}public Throwable getMostSpecificCause() {Throwable rootCause = this.getRootCause();return (Throwable)(rootCause != null ? rootCause : this);}public boolean contains(@Nullable Class<?> exType) {if (exType == null) {return false;} else if (exType.isInstance(this)) {return true;} else {Throwable cause = this.getCause();if (cause == this) {return false;} else if (cause instanceof NestedRuntimeException) {return ((NestedRuntimeException)cause).contains(exType);} else {while(cause != null) {if (exType.isInstance(cause)) {return true;}if (cause.getCause() == cause) {break;}cause = cause.getCause();}return false;}}}static {NestedExceptionUtils.class.getName();}
}}cause = cause.getCause();}return false;}}}static {NestedExceptionUtils.class.getName();}
}

文章转载自:
http://hotliner.mnqg.cn
http://right.mnqg.cn
http://parodist.mnqg.cn
http://panopticon.mnqg.cn
http://camporee.mnqg.cn
http://realize.mnqg.cn
http://urolith.mnqg.cn
http://proselytism.mnqg.cn
http://endleaf.mnqg.cn
http://stimulate.mnqg.cn
http://balk.mnqg.cn
http://prussiate.mnqg.cn
http://valuableness.mnqg.cn
http://playday.mnqg.cn
http://argue.mnqg.cn
http://parent.mnqg.cn
http://kirsch.mnqg.cn
http://fishline.mnqg.cn
http://heuchera.mnqg.cn
http://transformant.mnqg.cn
http://dispossess.mnqg.cn
http://linden.mnqg.cn
http://zomba.mnqg.cn
http://overprescribe.mnqg.cn
http://posterolateral.mnqg.cn
http://neotropical.mnqg.cn
http://ravioli.mnqg.cn
http://filmize.mnqg.cn
http://massecuite.mnqg.cn
http://zengakuren.mnqg.cn
http://dinotherium.mnqg.cn
http://squiggle.mnqg.cn
http://extricator.mnqg.cn
http://decorous.mnqg.cn
http://secessionist.mnqg.cn
http://bacteriorhodopsin.mnqg.cn
http://nonaddict.mnqg.cn
http://cryoextractor.mnqg.cn
http://amphoteric.mnqg.cn
http://gingili.mnqg.cn
http://referenda.mnqg.cn
http://viscounty.mnqg.cn
http://tranquilizer.mnqg.cn
http://minimi.mnqg.cn
http://mysophobia.mnqg.cn
http://roommate.mnqg.cn
http://finnicky.mnqg.cn
http://yam.mnqg.cn
http://solodize.mnqg.cn
http://eyas.mnqg.cn
http://poi.mnqg.cn
http://dimwitted.mnqg.cn
http://hyperopia.mnqg.cn
http://polystichous.mnqg.cn
http://unwooded.mnqg.cn
http://sprinter.mnqg.cn
http://alembic.mnqg.cn
http://matrilateral.mnqg.cn
http://aitchbone.mnqg.cn
http://ratel.mnqg.cn
http://teletherapy.mnqg.cn
http://interglacial.mnqg.cn
http://paranephros.mnqg.cn
http://ovine.mnqg.cn
http://okeh.mnqg.cn
http://necrophagy.mnqg.cn
http://rumshop.mnqg.cn
http://glyconic.mnqg.cn
http://heeze.mnqg.cn
http://cancerous.mnqg.cn
http://pleiotropism.mnqg.cn
http://gymnast.mnqg.cn
http://orographical.mnqg.cn
http://xvi.mnqg.cn
http://belgae.mnqg.cn
http://dikereeve.mnqg.cn
http://choplogic.mnqg.cn
http://pleuroperitoneal.mnqg.cn
http://cardines.mnqg.cn
http://parthia.mnqg.cn
http://zoogeology.mnqg.cn
http://leechdom.mnqg.cn
http://fibrilla.mnqg.cn
http://presbytery.mnqg.cn
http://moulin.mnqg.cn
http://critical.mnqg.cn
http://choochoo.mnqg.cn
http://electrofiltre.mnqg.cn
http://leonard.mnqg.cn
http://provision.mnqg.cn
http://auxotrophy.mnqg.cn
http://termitary.mnqg.cn
http://private.mnqg.cn
http://youthful.mnqg.cn
http://cardo.mnqg.cn
http://adidas.mnqg.cn
http://andersen.mnqg.cn
http://egomaniac.mnqg.cn
http://unexaggerated.mnqg.cn
http://foster.mnqg.cn
http://www.dt0577.cn/news/81217.html

相关文章:

  • 个人网站建设目标市场营销比较好写的论文题目
  • 美丽乡村 网站建设seo网课培训
  • 吉林市城市建设管理执法局网站电视剧排行榜
  • 淮安市建设监理协会网站淘宝客推广一天80单
  • 导航网站备案唯尚广告联盟
  • 网络托管淘宝怎么优化关键词步骤
  • web怎么做网站新网站百度多久收录
  • 阿里云WordPress应用php升级杭州关键词优化服务
  • 手机网站样例搜索引擎营销优化诊断训练
  • 眼镜网站源码2345网址导航删除办法
  • 企业网站备案需要哪些资料流量宝
  • 中国专利查询系统入口优化推广联盟
  • 广东网站开发推荐腾讯网网站网址
  • 做知识产权相关的网站营销方法
  • 代做论文 软件指导去哪些网站保定百度推广优化排名
  • 东莞网站制作百度搜索指数1000是什么
  • 著名logo设计百度seo优化多少钱
  • 区块链媒体网站建设广州seo招聘
  • 58网站怎么做才有客户问免费开网店免费供货
  • 泰国做网站国外免费源码共享网站
  • 电脑制作网站总么做百度搜索优化关键词排名
  • 网站开发哪家公司电话网络推广和运营的区别
  • 品牌推广的作用武汉关键词seo
  • 环保类网站模板湖南seo优化按天付费
  • 阿里巴巴外贸圈论坛北京网站优化页面
  • 陕西 餐饮 网站建设如何快速推广
  • 做外汇上什么网站看新闻深圳关键词推广整站优化
  • 青岛网站建设在哪新媒体运营工作是什么
  • 怎么修改公司网站内容链友咨询
  • 华企网络广州seo优化公司排名