异常处理

来源:互联网 发布:misumi软件介绍 编辑:程序博客网 时间:2024/06/06 01:22

Throwable类是异常类的根类,它有两个子类,分别是Error和Exception类

Error是JVM错误,与系统有关,Exception是用户编写程序,并且可以捕获恢复的错误。分为两种类型:

1。运行时异常,Runtime Exception,大多runtime exception是不需要编译器进行检查的。

如:

public class ExceptionDemo01{

 public static void main(String[]args){

  System.out.println(1/0);
 }
}

这个程序编译没有问题,运行时就会出现ArithmeticException

2。非运行时异常,必须要try{}catch{}块,或者用throws声明,否则编译出错。

比如说对文件操作时候,必须导入IO包,并且编写相应的try{}catch{}块.