error和exception

来源:互联网 发布:罗马人和日耳曼人 知乎 编辑:程序博客网 时间:2024/06/07 02:08

若有错误,请多指教!

error

an error is a subclass of Throwable that indicates serous problem that a reasonable application should not try to catch
这是jdk源代码上的解释,error代表的是严重的错误,到底严重到什么程度呢?这分为编译错误和运行时错误,编译错误包含漏掉括号、分号、没有导入相应包等等有红线提示的错误。运行时错误,包含OutOfMemoryError、StackOverflowError、ExceptionInInitializerError等。这两种错误都有一个特点就是,一旦出现,程序就不能执行,是系统级别的、程序员不可捕捉并进行处理的。

exception

The class {@code Exception} and its subclasses are a form of {@code Throwable} that indicates conditions that a reasonable application might want to catch.
出现except错误,可能是程序员希望捕捉并处理的,分为runtime exception和cheked exception

  • 顾名思义,runtime exception指的是运行时错误,如NullPointerException、IndexOutOfBoundsException等,对于此异常,我们一般不做处理,而是交给虚拟机,但会造成所在的线程终止;若我们不想让其终止,我们可以捕获其错误,并记录在日志中,保证程序和正常运行和错误检查。此错误一般是程序员造成的,一般可以消除。
  • checked exception。检查型错误,必须捕捉并自行处理,如IOException 、SQLException、自定义的LoinException等,此错误一般不可预测,加入此检查错误是为了保证程序的正常运行而不是预防错误,如我们注册时有密码设置异常处理来保证我们输入的密码在8到20位之间且存在数字大小写字母等。

参考

Difference between Error and Exception

原创粉丝点击