论坛首页 Java企业应用论坛

讨论关于项目中异常的处理

浏览 6333 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2004-12-28  
我去面试的时候经常遇到项目中异常处理的问题,
现在的项目中,比如说SQLException都没有向上抛。就是自己程序处理。
项目中用的struts+dao,看了IBM的那篇高级DAO编程深有感触(文章中就说DAO中不能抛出SQLException).不知道大家有什么看法?
/**
此断注销

在程序中保持良好的日志习惯,现在我测试log4j的时候把配置文件放到上一级目录中,不知道怎么读取,是否要指定为绝对路径呢?(如果需要指定为绝对路径的话,那项目部署的时候会很麻烦,有知道的朋友请告知)还有如果项目中加入log4j,那整个项目中是否只需要一份log4j的配置文件呢?如果在不同的包中怎么调用配置文件呢?
*/
   发表时间:2004-12-28  
不知道这帖子属不属于删贴的范畴啊???

...

算了,还是回答一下。
要读取包内properties文件,用getClass.getResourceAsStream(String url)就行了。
0 请登录后投票
   发表时间:2004-12-29  
如果用Struts+DAO,分层编码,Exception不抛出来的话,上一层怎么得知下一层出了什么错?
0 请登录后投票
   发表时间:2004-12-29  
不是不抛异常,而是最好不抛SQLException
0 请登录后投票
   发表时间:2004-12-30  
自己创建一个nochecked Exception基类
然后继承出两个子类,一个BusinessException,一个DataAccessException
BusinessException里的message提示给用户,DataAccessException直接
提示系统异常.
当然这个nochecked Exception基类要支持异常链,eg.
package sitech.www.frame.util;

import java.lang.*;
import java.io.*;

public class SysException
    extends RuntimeException {

  public final static boolean release14;
  // A wrapped Throwable
  protected Throwable cause = null;

  protected static int errCode = -1;

  static {
    String vers = System.getProperty("java.version");;
    if (vers.compareTo("1.4.0"); >= 0); {
      release14 = true;
    }
    else {
      release14 = false;
    }
  }

  public SysException(String message); {
    super(message);;
  }

  public SysException(int ec, String message); {
    super(message);;
    errCode = ec;
  }

  public SysException(String message, Throwable cause); {
    super(message);;
    this.cause = cause;
  }

  public SysException(int ec, String message, Throwable cause); {
    super(message);;
    errCode = ec;
    this.cause = cause;
  }

  public void printStackTrace(); {
    // Print the stack trace for this exception.
    super.printStackTrace();;
    if (!release14 && cause != null); {
      System.err.println("caused by:");;
      cause.printStackTrace();;
    }
  }

  public void printStackTrace(PrintStream s); {
    // Print the stack trace for this exception.
    super.printStackTrace(s);;
    if (!release14 && cause != null); {
      s.println("caused by:");;
      cause.printStackTrace(s);;
    }
  }

  public void printStackTrace(PrintWriter w); {
    // Print the stack trace for this exception.
    super.printStackTrace(w);;
    if (!release14 && cause != null); {
      w.println("caused by:");;
      cause.printStackTrace(w);;
    }
  }

  /**
   * return errCode with system level
   * @return default -1 unclassed exception
   */
  public int getErrorCode(); {
    return errCode;
  }

  /**
   * get the exception which this caseed by
   * @return source exception
   */
  public Throwable getCause(); {
    return cause;
  }
}
0 请登录后投票
   发表时间:2004-12-30  
似乎多次一举!
0 请登录后投票
   发表时间:2004-12-31  
谢谢!最好谁能写一个Demo出来,给大家评评
0 请登录后投票
   发表时间:2005-01-08  
异常无外乎两种:系统异常和业务异常,业务异常肯定是直接抛出的,至于系统异常呢,我认为在开发阶段最好还是直接抛出,要不然找出错的地方找到你死;到了要进行集成的时候,就要把系统异常封装成自定义的系统异常,出于三个方面的考虑:
1、系统安全考虑,好比sqlException,如果直接抛出,则用户有可能看到你系统的表结构;
2、封装后能进行统一的处理,即使目前不处理,但也为以后的处理作好准备;
3、样子好看点了,要知道如果用户看到一大堆乱七八糟的exception语句是会呱呱大叫的。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics