栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java中的Exception

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java中的Exception

Java中的Exceptions主要分:checked exception 和 unchecked exception。我们会分别介绍这两类异常及其使用方法。

Checked Exceptions

从Exception给出的定义,我们可以看到:checked exceptions继承于java.lang.Exception。

The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions

checked exceptions必须用try catch显式地捕获或者throws声明抛出异常。

常见的Checked Exceptions有IOException,SQLException,ClassNotFoundException。还有些常用的exception是继承于IOException的,比如FileNotFoundException,MalformedURLException等等。

有时候在使用引用的时候,但是如果调用的方法抛出了checked exception,我们会发现在调用的时候报错,unhandled exception.

Unchecked Exceptions

同样地,我们也可以从RuntimeException看到unchecked exceptions的介绍,unchecked Exceptions继承于java.lang.RuntimeException。

RuntimeException and its subclasses are unchecked exceptions.

unchecked exceptions不必捕获异常或者抛出。

经常使用的unchecked exceptions有IllegalArgumentException,NullPointerException,IndexOutOfBoundsException,

选择Checked Exceptions 还是 Unchecked Exceptions

对于checked excpetions,编译器会显式地提醒,这样我们就不会忘记处理异常。

有时候出现异常,我们会习惯写log,但是对于RuntimeException可以不写在log里面。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/349095.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号