try
{
  method1();  //method1抛出异常
}
catch(Exception e)
{
    e.printStackTrace();
}这种异常写法有问题异常捕获没做动作,在网上找答案得到下列结果
(1) 处理异常,进行修复以让程序继续执行。
(2) 重新抛出异常,在对异常进行分析后发现这里不能处理它,那么重新抛出异常,让调用者处理。
(3) 将异常转换为用户可以理解的自定义异常再抛出,这时应该注意不要丢失原始异常信息。
(4) 不要捕获异常。
根据这4中大概怎么去写啊?(1)怎么处理,在catch中怎么做?
(2)重新抛出异常怎么抛出?throw e 吗?报错。 throw new IllegalArgumentException("异常") 这样 原始异常信息又丢失了。
(3)如果throw e  和 throws 同时使用,哪个起作用?

解决方案 »

  1.   

    1.在try中做些你认为可用的逻辑,比如记录log,
    2.throw e可以把原始异常抛出,跟没有try效果一样throw new IllegalArgumentException("异常",e),另外的构造器可以把原始异常带下去。
    3.throws只是方法上声明会有异常抛出,throw是真正碰到异常的一个往外扔的动作。
    http://blog.csdn.net/ronawilliam/article/details/3299676 
      

  2.   

    catch到异常,一般会做一些日志记录,然后把异常往上抛
      

  3.   

    public static void main(String[] args) {

    try {
    new Test().t();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    System.out.println("抛出异常先执行");
    }

    }

    public void t() throws Exception{

    try {
    this.getClass().getClassLoader().loadClass("com");
    } catch (ClassNotFoundException e) {
    System.out.println("捕获异常先执行");
    e.printStackTrace();
    }
    }
    实验是检验真理的唯一标准。。
    不能处理的异常就不要捕获
      

  4.   

    楼上的 public void t() throws Exception{  这个方法不是 可以抛出异常吗。main 怎么没捕获到
      

  5.   

      已经被try{}catch(){}捕获了,throws 与try catch不要一起用容易混乱。
      

  6.   

       public void t()  throws Exception{        try {
            this.getClass().getClassLoader().loadClass("com");
            } catch (ClassNotFoundException e) {
            System.out.println("捕获异常先执行");
            throw e;
            }
            }   这样 如果不加 throws Exception  编译会报错