标签:
中断了正常指令流的事件。
异常是一个对象 ,在出现异常时,虚拟机会生成一个异常对象
生成对象的类是由 JDK 提供的
上图解释:
Throwable 是所有异常类的基类
Error 是指虚拟机在运行时产生的错误,在出现错误时,虚拟机会关闭
Exception 指异常
RuntimeException 运行时异常(也可称为 uncheck Exception )、
e.g :
class Test{ public static void main(String args[]){ int i= 1/0; } }
错误信息:
错误解释:
在主线程中出现异常 名字为ArithmenticException 即算术异常
:后面为 异常的信息 at后表示位置
.... 代表 check Exception 编译时就会出错
e.g:
class Test{ public static void main(String args[]){ Thread.sleep(1000); } }
为避免不友好的错误 ,提高程序的健壮性
引入try catch() finally
代码:
class Test{
public static void main(String args[]){
try{
System.out.println("1");
int i = 1/0;
System.out.println("2");
}
catch(Exception e){
e.printStackTrace();
System.out.println("3");
}
finally{
System.out.println("4");
}
}
}
易错的代码放在try里 错误处理放在catch里 finally里放清理资源的代码
标签:
原文地址:http://www.cnblogs.com/lipeng0824/p/4381952.html