当前位置: 代码迷 >> Java相关 >> 我这个程序怎么就运行不出来?
  详细解决方案

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

热度:357   发布时间:2008-05-25 13:38:14.0
我这个程序怎么就运行不出来?
实验要求:
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);
        }
    }
}
搜索更多相关的解决方案: 运行  

----------------解决方案--------------------------------------------------------
你的catch语句中的异常代码要连上!
你的data是什么类阿?
----------------解决方案--------------------------------------------------------
Data是什么东西?
----------------解决方案--------------------------------------------------------
把第二个程序改成这样的就行了
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);
        }
    }
}
----------------解决方案--------------------------------------------------------
  相关解决方案