您的位置:首页 > 编程语言 > Java开发

初学java心得及 所犯的错误

2014-02-19 18:22 316 查看
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异常")
}
以上是解决程序的异常的方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: