package Exception.test;
/**
* 异常处理:当程序运行的时候,如果程序由于异常停止了,那么后面的程序将不会执行,整个项目将处理崩溃
* 异常类的最终父类:java.lang.Throwable
Throwable的直接子类:
1.Error(错误)
2.Exception(异常) 我们主要处理此种异常
检查性异常(checked exception)这种异常一定要处理
若系统运行时可能产生该类异常,则必须写出相应的处理代码,否则无法通过编译
非RuntimeException异常
非检查性异常(unchecked exception)无需处理,程序员可以自己完成(Debug)
若系统运行时可能产生该类异常,则不必在程序中声明对该类,异常的处理,就可以编译执行
RuntimeException:运行时异常
如何处理异常
try {
// 有可能产生异常的代码
} catch (Exception e) {
// 捕获异常并处理
}
通常来说非检查性异常不用try.....catch
*
*/
public class Test1123 {
public static void main(String[] args) {
int i = 0;
String greetings[] = { "Hello World", "Hello Dingdang", "Hello Kitty" };
// while (i < 4) {
// System.out.println(greetings[i]);
// i++;
// }
// System.out.println("adsfasdfasdfasdf");
try {
//有可能出现异常的代码
while (i < 4) {
System.out.println(greetings[i]);
i++;
}
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println("数组下标越界了!!!!!!");
}catch (RuntimeException e) {
// TODO: handle exception
}catch (Exception e) {
// TODO: handle exception
}finally {
//无论以上代码是否出现异常都会执行finally块中的代码
//主要的作用可以释放资源(关闭数据库的连接)
System.out.println("最终都会执行finally....");
}//异常 从子类-->父类
System.out.println("adsfasdfasdfasdf");
}
}