[Java] Java异常相关的问题 →→→→→进入此内容的聊天室

来自 , 2020-06-17, 写在 Java, 查看 159 次.
URL http://www.code666.cn/view/8e2cfdc2
  1. package app.creditapp.db;
  2. import java.io.IOException;
  3. import java.sql.SQLException;
  4. import org.apache.log4j.Logger;
  5. import app.util.exc.ExceptionHandlerImpl;
  6. import app.util.exc.NoSuchEntityException;
  7.  
  8. public class PublicDAOExHandler extends ExceptionHandlerImpl {
  9.         public PublicDAOExHandler() {
  10.         }
  11.         static Logger logger = Logger.getLogger(PublicDAOExHandler.class);
  12.         public void handle(Exception e) {
  13.                 e.printStackTrace();
  14.                 logger.error("进入到handle(Exception e)异常处理中::"+e.getMessage());
  15.                 if (e instanceof SQLException) {
  16.                         logger.error("sql的错误代码为::"+((SQLException) e).getErrorCode());
  17.                        
  18.                          if (((SQLException) e).getErrorCode() == -255  ) { //数据库服务器不执行该COMMIT WORK或ROLLBACK WORK语句
  19.                                
  20.                                  logger.error("error sqlException 原因为 :::  数据库服务器不执行该COMMIT WORK或ROLLBACK WORK语句");
  21.                                 throw new NoSuchEntityException("error.COMMITandROLLBACK");
  22.                
  23.                          }  else if (((SQLException) e).getErrorCode() == -220) { // 。   1
  24.                        
  25.                                  logger.error(" error sqlException 原因为 ::: 查询中没有FROM子句");
  26.                                 throw new NoSuchEntityException("error.from.keyword.not.found");
  27.                
  28.                          }else if (((SQLException) e).getErrorCode() == -235) { // 字符字段的宽度太大。 2 \u5b57\u7b26\u5b57\u6bb5\u7684\u5bbd\u5ea6\u592a\u5927
  29.                        
  30.                                  logger.error("error sqlException 原因为 ::: 字符字段的宽度太大");
  31.                                 throw new NoSuchEntityException("error.char.long");
  32.                
  33.                          }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
  34.                        
  35.                                  logger.error("error sqlException 原因为 ::: 在INSERT中字段数与VALUES的数目不匹配");
  36.                                 throw new NoSuchEntityException("error.insert.values.not.matching");
  37.                        
  38.                          }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
  39.                        
  40.                                  logger.error("error sqlException 原因为 ::: 在UPDATE中的字段数与VALUES的数目不匹配");
  41.                                 throw new NoSuchEntityException("error.update.values.not.matching");
  42.                        
  43.                          } else if (((SQLException) e).getErrorCode() == -239) { //不能插入新行 — 在UNIQUE INDEX字段中有重复值。
  44.                        
  45.                                  logger.error("error sqlException 原因为 ::: 不能插入新行 — 在UNIQUE INDEX字段中有重复值");
  46.                                 throw new NoSuchEntityException("error.dupKey");
  47.                
  48.                          }else if (((SQLException) e).getErrorCode() == -268) { //不能插入新行 — 违反主键约束。
  49.                                        
  50.                                  logger.error("error sqlException 原因为 ::: 不能插入新行 — 违反主键约束");
  51.                                 throw new NoSuchEntityException("error.dupKey");
  52.                
  53.                          }else if (((SQLException) e).getErrorCode() == -286) { //主键字段<字段名>的缺省值是NULL。
  54.                
  55.                                  logger.error("error sqlException 原因为 ::: 主键字段<字段名>的缺省值是NULL");
  56.                                 throw new NoSuchEntityException("error.can.not.be.null");
  57.                
  58.                          }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
  59.                
  60.                                  logger.error("error sqlException 原因为 :::  WHERE子句包含一个外部笛卡尔乘积");
  61.                                 throw new NoSuchEntityException("error.where.Cartesian");
  62.                
  63.                          }else if (((SQLException) e).getErrorCode() == -650) { //最大的varchar大小已经被超越。\u6700\u5927\u7684\u0076\u0061\u0072\u0063\u0068\u0061\u0072\u5927\u5c0f\u5df2\u7ecf\u88ab\u8d85\u8d8a
  64.                        
  65.                                  logger.error("error sqlException 原因为 :::  最大的varchar大小已经被超越");
  66.                                 throw new NoSuchEntityException("error.varchar.this.column");
  67.                
  68.                          }else if (((SQLException) e).getErrorCode() == -836    ) { //插入语句没有values子句。
  69.                        
  70.                                  logger.error("error sqlException 原因为 :::  插入语句没有values子句");
  71.                                 throw new NoSuchEntityException("error.missing.values.keyword");
  72.                        
  73.                          }else if (((SQLException) e).getErrorCode() == -839    ) { //表未被找到。
  74.                
  75.                                  logger.error("error sqlException 原因为 :::  表未被找到");
  76.                                 throw new NoSuchEntityException("error.table.or.view.does.not.exist");
  77.                
  78.                          }else if (((SQLException) e).getErrorCode() == -206    ) { //表在数据库中未被找到。
  79.                                        
  80.                                  logger.error("error sqlException 原因为 :::  表在数据库中未被找到");
  81.                                 throw new NoSuchEntityException("error.table.or.view.does.not.exist");
  82.                
  83.                          }else if (((SQLException) e).getErrorCode() == -201    ) { //发生了一个语法错误。 \u53d1\u751f\u4e86\u4e00\u4e2a\u8bed\u6cd5\u9519\u8bef
  84.                
  85.                                  logger.error("error sqlException 原因为 :::  发生了一个语法错误");
  86.                                 throw new NoSuchEntityException("error.solecism");
  87.                
  88.                          }else if (((SQLException) e).getErrorCode() == -202    ) { //语句中含义非法字符 如 " ' "。
  89.                
  90.                                  logger.error("error sqlException 原因为 :::  语句中含义非法字符 如  ' ");
  91.                                 throw new NoSuchEntityException("error.ffzf");
  92.                
  93.                          }else{
  94.                        
  95.                                  logger.error("error sqlException 原因为 ::: 没有捕获的sql异常");
  96.                                 throw new NoSuchEntityException("error.unexpectedSqlError");
  97.                
  98.                          }
  99.                
  100.                 } else if (e instanceof ArithmeticException) {//算数条件异常
  101.                
  102.                         logger.error("error  ArithmeticException ::: 算数条件异常");
  103.                         throw new NoSuchEntityException("error.ArithmeticException");
  104.                
  105.                 } else if (e instanceof ArrayIndexOutOfBoundsException) {//数组越界异常
  106.                
  107.                         logger.error("error  ArrayIndexOutOfBoundsException ::: 数组越界异常");
  108.                         throw new NoSuchEntityException(
  109.                                         "error.ArrayIndexOutOfBoundsException");
  110.                
  111.                 } else if (e instanceof ClassCastException) {//类型转换异常
  112.                
  113.                         logger.error("error  ArrayIndexOutOfBoundsException ::: 类型转换异常");
  114.                         throw new NoSuchEntityException("error.ClassCastException");
  115.                
  116.                 } else if (e instanceof IllegalAccessException) {//但当前正在执行的方法无法访问指定类、字段、方法或构造方法的定义时
  117.                
  118.                         logger.error("error  IllegalAccessException ::: 但当前正在执行的方法无法访问指定类、字段、方法或构造方法的定义时异常");
  119.                         throw new NoSuchEntityException("error.IllegalAccessException");
  120.                
  121.                 } else if (e instanceof IOException) {//I/O异常
  122.                
  123.                         logger.error("error  IOException ::: I/O异常");
  124.                         throw new NoSuchEntityException("error.IOException");
  125.        
  126.                 } else if (e instanceof NullPointerException) {//空指针异常
  127.                
  128.                         logger.error("error  NullPointerException ::: 空指针异常");
  129.                         throw new NoSuchEntityException("error.NullPointerException");
  130.                
  131.                 } else if (e instanceof RuntimeException) { //运行中的其他异常
  132.                
  133.                         logger.error("error  RuntimeException ::: 运行中的其他异常");
  134.                         throw new NoSuchEntityException(e.getMessage());
  135.        
  136.                 }else if (e instanceof RuntimeException) { //运行中的其他异常
  137.                
  138.                         logger.error("error  UnknownHostException ::: 运行中的其他异常");
  139.                         throw new NoSuchEntityException("error.host");
  140.              
  141.                  }  else if ("appno".equals(e.getMessage())) { //运行中的其他异常
  142.                                
  143.                                 logger.error("error  error.repeat.apply ::: 该笔申请在合同中已经存在");
  144.                                 throw new NoSuchEntityException("error.repeat.apply");
  145.                      
  146.                } else {//未知错误请与管理员联系
  147.                    
  148.                         logger.error("error   unexpected ::: 未知错误请与管理员联系");
  149.                         throw new NoSuchEntityException("error.unexpected");
  150.                
  151.                 }
  152.         }
  153. }//源代码片段来自云代码http://yuncode.net
  154.                        

回复 "Java异常相关的问题"

这儿你可以回复上面这条便签

captcha