简体   繁体   中英

Java checked and unchecked exceptions

  1. If I create an exception class that extends Exception , will my class be checked or unchecked? I note that the subclass of Exception called RuntimeException is an unchecked exception whereas all other subclasses of 'Exception' are checked exceptions.

  2. If I create an exception class that extends RuntimeException , can I specify that this class is checked?

1) Checked

2) No

If you extend Exception -> checked

If you extend RuntimeException -> unchecked

From documentation:

The class {@code Exception} and any subclasses that are not also * subclasses of {@link RuntimeException} are checked * exceptions

在此输入图像描述

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

If you create class that extends Exception it would be checked. You can't specify RuntimeException as checked since it is unchecked exception

If your class extends Exception it can throw checked exceptions.

If your class extends Error or RuntimeException it can throw unchecked exceptions.

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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