初学java心得及 所犯的错误

来源:互联网 发布:vc socket编程 重连 编辑:程序博客网 时间:2024/06/05 16:44

1.Inter inter = new A();//向上转型

          B objB=(B)inter;//向下转型

以上为运行异常即为runtime exception,A不能转为B

2.Student  stu[]=new Student[3]; 

        Scanner sc=new Scanner(System.in);

        System.out.println("请输入学生姓名:");//由于学生数组中没有学生的名字

        stu[0].name=sc.nextLine();

         int array[]={1,2,3};

          System.out.println(array[3]);//由于下标越箭了,故会出错,即异常

以上错误为stu【】数组为空为故为checked exception,一般要自己解决,不过也能向上抛(一级一级的抛),如果是main方法则抛给虚拟机

3.try { int intVar = array[3];

}catch(ArrayIndexOutOfBoundsException e){// try-catch处理当前类型的异常,ArrayIndexOutOfBoundsException e为错误代码

System.out.println("数组下标越界");//

          }catch(Exception e){//若不是上一类则在这处理若还不是则往下继续找

System.out.println("不明原因exception异常")

          }

以上是解决程序的异常的方法


0 0
原创粉丝点击