package app.creditapp.db; import java.io.IOException; import java.sql.SQLException; import org.apache.log4j.Logger; import app.util.exc.ExceptionHandlerImpl; import app.util.exc.NoSuchEntityException; public class PublicDAOExHandler extends ExceptionHandlerImpl { public PublicDAOExHandler() { } static Logger logger = Logger.getLogger(PublicDAOExHandler.class); public void handle(Exception e) { e.printStackTrace(); logger.error("进入到handle(Exception e)异常处理中::"+e.getMessage()); if (e instanceof SQLException) { logger.error("sql的错误代码为::"+((SQLException) e).getErrorCode()); if (((SQLException) e).getErrorCode() == -255 ) { //数据库服务器不执行该COMMIT WORK或ROLLBACK WORK语句 logger.error("error sqlException 原因为 ::: 数据库服务器不执行该COMMIT WORK或ROLLBACK WORK语句"); throw new NoSuchEntityException("error.COMMITandROLLBACK"); } else if (((SQLException) e).getErrorCode() == -220) { // 。 1 logger.error(" error sqlException 原因为 ::: 查询中没有FROM子句"); throw new NoSuchEntityException("error.from.keyword.not.found"); }else if (((SQLException) e).getErrorCode() == -235) { // 字符字段的宽度太大。 2 \u5b57\u7b26\u5b57\u6bb5\u7684\u5bbd\u5ea6\u592a\u5927 logger.error("error sqlException 原因为 ::: 字符字段的宽度太大"); throw new NoSuchEntityException("error.char.long"); }else if (((SQLException) e).getErrorCode() == -236) { //在INSERT中字段数与VALUES的数目不匹配。 2\u5728\u0049\u004e\u0053\u0045\u0052\u0054\u4e2d\u5b57\u6bb5\u6570\u4e0e\u0056\u0041\u004c\u0055\u0045\u0053\u7684\u6570\u76ee\u4e0d\u5339\u914d logger.error("error sqlException 原因为 ::: 在INSERT中字段数与VALUES的数目不匹配"); throw new NoSuchEntityException("error.insert.values.not.matching"); }else if (((SQLException) e).getErrorCode() == -505) { //在UPDATE中的字段数与VALUES的数目不匹配。2\u5728\u0055\u0050\u0044\u0041\u0054\u0045\u4e2d\u7684\u5b57\u6bb5\u6570\u4e0e\u0056\u0041\u004c\u0055\u0045\u0053\u7684\u6570\u76ee\u4e0d\u5339\u914d logger.error("error sqlException 原因为 ::: 在UPDATE中的字段数与VALUES的数目不匹配"); throw new NoSuchEntityException("error.update.values.not.matching"); } else if (((SQLException) e).getErrorCode() == -239) { //不能插入新行 — 在UNIQUE INDEX字段中有重复值。 logger.error("error sqlException 原因为 ::: 不能插入新行 — 在UNIQUE INDEX字段中有重复值"); throw new NoSuchEntityException("error.dupKey"); }else if (((SQLException) e).getErrorCode() == -268) { //不能插入新行 — 违反主键约束。 logger.error("error sqlException 原因为 ::: 不能插入新行 — 违反主键约束"); throw new NoSuchEntityException("error.dupKey"); }else if (((SQLException) e).getErrorCode() == -286) { //主键字段<字段名>的缺省值是NULL。 logger.error("error sqlException 原因为 ::: 主键字段<字段名>的缺省值是NULL"); throw new NoSuchEntityException("error.can.not.be.null"); }else if (((SQLException) e).getErrorCode() == -395) { //WHERE子句包含一个外部笛卡尔乘积。\u0057\u0048\u0045\u0052\u0045\u5b50\u53e5\u5305\u542b\u4e00\u4e2a\u5916\u90e8\u7b1b\u5361\u5c14\u4e58\u79ef logger.error("error sqlException 原因为 ::: WHERE子句包含一个外部笛卡尔乘积"); throw new NoSuchEntityException("error.where.Cartesian"); }else if (((SQLException) e).getErrorCode() == -650) { //最大的varchar大小已经被超越。\u6700\u5927\u7684\u0076\u0061\u0072\u0063\u0068\u0061\u0072\u5927\u5c0f\u5df2\u7ecf\u88ab\u8d85\u8d8a logger.error("error sqlException 原因为 ::: 最大的varchar大小已经被超越"); throw new NoSuchEntityException("error.varchar.this.column"); }else if (((SQLException) e).getErrorCode() == -836 ) { //插入语句没有values子句。 logger.error("error sqlException 原因为 ::: 插入语句没有values子句"); throw new NoSuchEntityException("error.missing.values.keyword"); }else if (((SQLException) e).getErrorCode() == -839 ) { //表未被找到。 logger.error("error sqlException 原因为 ::: 表未被找到"); throw new NoSuchEntityException("error.table.or.view.does.not.exist"); }else if (((SQLException) e).getErrorCode() == -206 ) { //表在数据库中未被找到。 logger.error("error sqlException 原因为 ::: 表在数据库中未被找到"); throw new NoSuchEntityException("error.table.or.view.does.not.exist"); }else if (((SQLException) e).getErrorCode() == -201 ) { //发生了一个语法错误。 \u53d1\u751f\u4e86\u4e00\u4e2a\u8bed\u6cd5\u9519\u8bef logger.error("error sqlException 原因为 ::: 发生了一个语法错误"); throw new NoSuchEntityException("error.solecism"); }else if (((SQLException) e).getErrorCode() == -202 ) { //语句中含义非法字符 如 " ' "。 logger.error("error sqlException 原因为 ::: 语句中含义非法字符 如 ' "); throw new NoSuchEntityException("error.ffzf"); }else{ logger.error("error sqlException 原因为 ::: 没有捕获的sql异常"); throw new NoSuchEntityException("error.unexpectedSqlError"); } } else if (e instanceof ArithmeticException) {//算数条件异常 logger.error("error ArithmeticException ::: 算数条件异常"); throw new NoSuchEntityException("error.ArithmeticException"); } else if (e instanceof ArrayIndexOutOfBoundsException) {//数组越界异常 logger.error("error ArrayIndexOutOfBoundsException ::: 数组越界异常"); throw new NoSuchEntityException( "error.ArrayIndexOutOfBoundsException"); } else if (e instanceof ClassCastException) {//类型转换异常 logger.error("error ArrayIndexOutOfBoundsException ::: 类型转换异常"); throw new NoSuchEntityException("error.ClassCastException"); } else if (e instanceof IllegalAccessException) {//但当前正在执行的方法无法访问指定类、字段、方法或构造方法的定义时 logger.error("error IllegalAccessException ::: 但当前正在执行的方法无法访问指定类、字段、方法或构造方法的定义时异常"); throw new NoSuchEntityException("error.IllegalAccessException"); } else if (e instanceof IOException) {//I/O异常 logger.error("error IOException ::: I/O异常"); throw new NoSuchEntityException("error.IOException"); } else if (e instanceof NullPointerException) {//空指针异常 logger.error("error NullPointerException ::: 空指针异常"); throw new NoSuchEntityException("error.NullPointerException"); } else if (e instanceof RuntimeException) { //运行中的其他异常 logger.error("error RuntimeException ::: 运行中的其他异常"); throw new NoSuchEntityException(e.getMessage()); }else if (e instanceof RuntimeException) { //运行中的其他异常 logger.error("error UnknownHostException ::: 运行中的其他异常"); throw new NoSuchEntityException("error.host"); } else if ("appno".equals(e.getMessage())) { //运行中的其他异常 logger.error("error error.repeat.apply ::: 该笔申请在合同中已经存在"); throw new NoSuchEntityException("error.repeat.apply"); } else {//未知错误请与管理员联系 logger.error("error unexpected ::: 未知错误请与管理员联系"); throw new NoSuchEntityException("error.unexpected"); } } }//源代码片段来自云代码http://yuncode.net