初学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异常")
}
以上是解决程序的异常的方法
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异常")
}
以上是解决程序的异常的方法
相关文章推荐
- 初学JAVA心得
- 初学Java的一点点心得
- JAVA初学的几个小错误(命令行)
- JAVA初学笔记---控制台输入:错误scanner
- 初学Mybatis 构建sqlSession 错误问题 Error building SqlSession Mybatis java.lang.NullPointerException
- 用java编网页的学习流程,我的一些小心得(初学java到高深运用)
- 初学java构造方法错误解析
- 初学心得:Java连接SQL Server 2000
- 初学Java004 常见错误
- 一位初学java的学生的一些上课心得
- 初学JAVA的一些心得--Class类
- 带着问题去学习Java,初学Java的小菜自己的心得
- 初学java遇到的错误:Bad version nuber in .class file
- 初学JAVA心得---动机和行为
- 黑马程序员——初学java心得小总结
- Hibernate初学时遇见错误:Caused by: java.lang.ClassNotFoundException: javax.persistence.EntityListene
- java实验一ATM心得
- eclipse启动tomcat出现内存溢出错误 java.lang.OutOfMemoryError: PermGen space
- java.lang.UnsupportedOperationException: Not supported by DruidDataSource 错误解析