简体   繁体   English

Java已检查和未检查的异常

[英]Java checked and unchecked exceptions

  1. If I create an exception class that extends Exception , will my class be checked or unchecked? 如果我创建一个扩展Exception的异常类,我的类是否会被选中或取消选中? I note that the subclass of Exception called RuntimeException is an unchecked exception whereas all other subclasses of 'Exception' are checked exceptions. 我注意到名为RuntimeExceptionException的子类是一个未经检查的异常,而'Exception'的所有其他子类都是已检查的异常。

  2. If I create an exception class that extends RuntimeException , can I specify that this class is checked? 如果我创建一个扩展RuntimeException的异常类,我可以指定检查此类吗?

1) Checked 1)检查

2) No 2)没有

If you extend Exception -> checked 如果扩展Exception - > checked

If you extend RuntimeException -> unchecked 如果扩展RuntimeException - > unchecked

From documentation: 来自文档:

The class {@code Exception} and any subclasses that are not also * subclasses of {@link RuntimeException} are checked * exceptions 检查类{@code Exception}和任何不属于{@link RuntimeException}子类的子类*例外

在此输入图像描述

除非从RuntimeException或Error继承,否则将检查异常

If you create class that extends Exception it would be checked. 如果您创建扩展Exception的类,则会进行检查。 You can't specify RuntimeException as checked since it is unchecked exception 您不能将RuntimeException指定为已选中,因为它是未经检查的异常

If your class extends Exception it can throw checked exceptions. 如果您的类扩展了Exception,它可以抛出已检查的异常。

If your class extends Error or RuntimeException it can throw unchecked exceptions. 如果您的类扩展Error或RuntimeException,它可以抛出未经检查的异常。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM