zoukankan      html  css  js  c++  java
  • EJB 异常处理的最佳做法(1)

    http://www-900.ibm.com/developerWorks/cn/java/j-ejbexcept/index.shtml

    清单 1. 三种常见的异常处理做法

    100  try {
    101    OrderHome homeObj = EJBHomeFactory.getInstance().getOrderHome();
    102    Collection orderCollection = homeObj.findByCustomerId(id);
    103    iterator orderItter = orderCollection.iterator();
    104    while (orderIter.hasNext()) {
    105      Order orderRemote = (OrderRemote) orderIter.getNext();
    106      OrderValue orderVal = orderRemote.getValue();
    107      if (orderVal.getDate() < "mm/dd/yyyy") {
    108        OrderItemHome itemHome =
                  EJBHomeFactory.getInstance().getItemHome();
    109        Collection itemCol = itemHome.findByOrderId(orderId)
    110        Iterator itemIter = itemCol.iterator();
    111        while (itemIter.hasNext()) {
    112          OrderItem item = (OrderItem) itemIter.getNext();
    113          item.remove();
    114        }
    115        orderRemote.remove();
    116      }
    117    }
    118  } catch (NamingException ne) {
    119    throw new EJBException("Naming Exception occurred");
    120  } catch (FinderException fe) {
    121    fe.printStackTrace();
    122    throw new EJBException("Finder Exception occurred");
    123  } catch (RemoteException re) {
    124    re.printStackTrace();
    125    //Some code to log the message
    126    throw new EJBException(re);
    127  }

    1).抛出/重抛出带有出错消息的异常(不好,异常被吞)
    2).记录到控制台并抛出一个异常(不好,异常被吞)

    3).包装原始的异常以保护其内容

    RemoteException 可能发生在行 102、106、109、113 或 115。它在行 123 的 catch 块被捕获。接着,这个异常被包装到 EJBException 中,所以,不论调用者在哪里记录它,它都能保持完整。这种办法比前面两种办法更好,同时演示了没有日志策略的情况。如果 deleteOldOrders() 方法的调用者记录该异常,那么将导致重复记录。而且,尽管有了日志记录,但当客户报告某个问题时,产品日志或控制台并不能被交叉引用。


  • 相关阅读:
    Python 序列化处理
    httpclient
    java获取配置文件中变量值
    利用数据库管理工具(Navicat)导出数据到Excel表中
    如何确保发布的项目是最新的
    上传文件,重命名
    Mybatis plus中一个框多条件查询 SQL拼接
    用eclipse发布springboot项目
    使用SQL命令行更改数据库字段类型
    Java中查询某个日期下所有时间段的数据
  • 原文地址:https://www.cnblogs.com/huqingyu/p/63776.html
Copyright © 2011-2022 走看看