代码1:
public class ZeroDivideException extends Exception{
private int index = -1;

public ZeroDivideException(){}

public ZeroDivideException(String s){
super(s);
}

public ZeroDivideException(int index){
super("/ by zero");
this.index = index;
}

public int getIndex(){
return index;
}
}

代码2:

import java.io.IOException;public class TryBlockTest {
public static int divide(int[] array , int index) throws ZeroDivideException{
try{
System.out.println("\nFirst try block in divide() entered");
array[index + 2] = array[index]/array[index + 1];
System.out.println("Code at end of first try block in divide()");
return array[index + 2];
}catch(ArithmeticException a){
System.out.println("Arithmetic exception caught in divide()");
      throw new ZeroDivideException(index + 1);
}catch(ArrayIndexOutOfBoundsException a){
System.out.println("Index-out-of-bounds exception caught in divide()");
}
System.out.println("Executing code after try block in divide()");
return array[index + 2];
}

        public static void main(String[] args){
int[] x = {10, 5, 0};
try{
System.out.println("First try block in main() entered");
System.out.println("result = " + divide(x,0));
x[1] = 0;
System.out.println("result = " + divide(x,0));
x[1] = 1;
System.out.println("result = " + divide(x,1));
}catch(ZeroDivideException a){
int index = a.getIndex();
if(index > 0){
x[index] = 1;
x[index + 1] = x[index - 1];
System.out.println("Zero divisor corrected to " + x[index]);
}
}catch(ArithmeticException a){
System.out.println("Arithmetic exception caught in main()");
}
System.out.println("Outside first try block in main()");
System.out.println("\nPress Enter to exit");

}
}  当代码2出现异常的时候它是传什么数据给代码1的?并且又是运行呢

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【llm0528】截止到2008-06-30 19:12:11的历史汇总数据(不包括此帖):
    发帖数:32                 发帖分:680                
    结贴数:31                 结贴分:650                
    未结数:1                  未结分:30                 
    结贴率:96.88 %            结分率:95.59 %            
    值得尊敬
      

  2.   

    如果不知道传的具体是什么,总是心里不能十分理解,我像我写了一个构造函数,也知道它要传进来的是string类型的,可就是知道它具体传的是什么,这多难过啊~~
      

  3.   

    更改下:
    如果不知道传的具体是什么,总是心里不能十分理解,我想我写了一个构造函数,也知道它要传进来的是string类型的,可就是不知道它具体传的是什么,这多难过啊~~
      

  4.   

    从lz的代码看,你的string就是“/by zero”。
    因为你调用的一直是int参数的构造函数
    如果调用string构造,那就是你throw new 时传递的参数
    上述情况外,传递的是null
      

  5.   

    不传参数也行的,因为你new了一个自定义异常类的对象