编写一个能够重发一个异常的程序(通过循环来实现)

解决方案 »

  1.   

    叫你做实验你却发贴求代码   
    lz你真懒 和我一样懒
    你得意思我明白  我给个代码吧
     
    public   class   test 

    public   void   method1(int   x)   throws   Exception 

    try   

    System.out.println(" see ");
    method2(x); 
    System.out.println(" no see ");

    catch   (NegativeArraySizeException   e)   

    System.out.println( "Checkpoint   1 "); 



    finally   

    System.out.println( "Checkpoint   2 "); 

    System.out.println( "Checkpoint   3 "); 
    }  public   void   method2(int   x)   throws   Exception 

    if   (x   <   0) 

    throw   new   NegativeArraySizeException(); 

    }  static   public   void   main(String[]   args)   throws   Exception   

    test3   t   =   new   test3(); 
    for(int i =1;i<5;i++){
    t.method1(-55); 
    System.out.println("for "+i);
    }
    System.out.println( "Checkpoint   4 ");  }  }
      

  2.   

    这个有难度,抛出异常如果不被捕获,程序就会over了
    如果LZ是为了做异常测试,可以写一个专门抛出异常的方法,然后在需要测试异常的地方调用该方法就可以了
    public void throwErr(String msg) throws Exception {
        throw new Exception("User Exception: " + msg);
    }for (int i=0; i<10; i++) {
        try {
            throwErr("Test_" + i);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }