public class ExceptionTest3 {
public void go(int n) throws ArithmeticException {
int a = 9;
System.out.println(a = a / n);
} public static void main(String args[]) throws ArithmeticException{
ExceptionTest3 t = new ExceptionTest3();
t.go(0);
}
}

解决方案 »

  1.   

    在Myeclipse里面没看到抛出异常,但是在DOS下能看到!!还有就是:public class ExceptionTest2 {
    public void go(int n) {
    int a = 9;
    System.out.println(a = a / n);
    throw new ArithmeticException("fdgve"); //这个throw怎么有何没有结果都是一回事啊!! } public static void main(String args[]) {
    ExceptionTest2 t = new ExceptionTest2();
    try {
    t.go(0);
    } catch (ArithmeticException ae) {
    ae.printStackTrace();
    }
    }
    }
      

  2.   

    我在eclipse里跑第一段代码,抛出异常了。
    Exception in thread "main" java.lang.ArithmeticException: / by zero
    at ExceptionTest3.go(ExceptionTest3.java:4)
    at ExceptionTest3.main(ExceptionTest3.java:9)第二段代码:异常应该捕获。
    try{       System.out.println(a = a / n);
    }catch(ArithmeticException){
          throw new ArithmeticException("fdgve");
    }
     
    像楼主的代码,当传入的参数非0时,抛出的异常就是throw new语句抛出来的,它会输出"fdgve"信息。而传入0时,抛出的异常是由语句println抛出的。
      

  3.   

    你直接在方法后面加throws而不用try catch去捕捉的话异常会被抛给JVM,编译时能通过,但运行时JVM不会给你捕捉该异常,它会抛出该异常报错  public class ExceptionTest3  {
        public void go(int n) {
            int a = 9;
    try {
             System.out.println(a = a / n);
    }catch( ArithmeticException  e){
    System.out.println("除数不能为0");
    }
        }    public static void main(String args[])   {
            ExceptionTest3 t = new ExceptionTest3();
            t.go(0);
        }
    }
      

  4.   

    楼主上面的写法跟没写是一样的,改成这样public void go(int n) {
            int a = 9;
    if(n == 0)
       throw new ArithmeticException("fdgve");
            System.out.println(a = a / n);
        }
      

  5.   


    public class ExceptionTest2 {
      public void go(int n) throws ArithmeticException {
        int a = 9;
        // 这样写大哥
        try {
          System.out.println(a = a
              / n);
        } catch (Exception e) {
          throw new ArithmeticException("fdgve");
        }
      }  public static void main(String args[]) {
        ExceptionTest2 t = new ExceptionTest2();
        try {
          t.go(0);
        } catch (ArithmeticException ae) {
          ae.printStackTrace();
        }
      }
    }