xishuiwuqing 发表于 2008-5-25 13:38

我这个程序怎么就运行不出来?

实验要求:
1) 程序中生成并捕获到NegativeArraySizeException和IndexOutOfBoundsException类型的异常,并显示捕获到异常信息。
2) 在(1)的基础上生成并捕获到NullPointerException类型的异常,并显示捕获到异常信息。

我写的程序如下:
public class shiyan8_1 {
    public static void main(String[] args) {
        int a[]={1,2,3,4,5};
        try{
            int b[]=new int[-3];
            System.out.println(b);
        }
            catch(Negative Array Size Exception ex){
                System.out.println("数组最大下标不能为负数");
            }
         try {
            for (int i = 5; i >= 0; i--) {
                System.out.println(a[i]);
                }
                }
            catch (Index Out Of Bounds Exception ex) {
               System.out.println("超出范围而产生的一场数位:"+ex.getMessage());  
            }
          }
      }   






public class shiyan8_2 {
    public static void main(String[] args) {
        Data d=null;
        try{
            d=null;
            d.getTime();
        }
        catch(NullPointerException ex){
            System.out.println("空指针异常"+ex);
        }
    }
}

xiaoyu2006 发表于 2008-5-25 14:05

你的catch语句中的异常代码要连上!
你的data是什么类阿?

h229159701 发表于 2008-5-25 15:24

Data是什么东西?

xishuiwuqing 发表于 2008-5-26 09:02

把第二个程序改成这样的就行了

import java.util.Date;
public class shiyan8_2 {
    public static void main(String[] args) {
         Date d=null;
        try{
            d=null;
            d.getTime();
        }
        catch(NullPointerException ex){
            System.out.println("空指针异常"+ex);
        }
    }
}

页: [1]

编程论坛