加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如果抛出RuntimeException,它是否可以作为异常捕获?

发布时间:2020-12-15 02:04:09 所属栏目:Java 来源:网络整理
导读:如果我有一个抛出RuntimException子类的try块,后续的catch块是否可以将其作为异常捕获?特别: public class MyAppException extends RuntimeException { // ....}// In some other part of the code:try { // Executing this results with doSomething() th
如果我有一个抛出RuntimException子类的try块,后续的catch块是否可以将其作为异常捕获?特别:

public class MyAppException extends RuntimeException {
    // ....
}

// In some other part of the code:
try {
    // Executing this results with doSomething() throwing a MyAppException.
    int x = doSomething();
} catch(Exception exc) {
    // Does the thrown MyAppException get caught here?
}

我的想法是肯定的,因为RuntimeException扩展了Exception.但是我有一些不符合这种方式的生产代码.很明显,如果答案是否定的,那就是我的答案;否则我需要深入了解为什么我的代码坏了.提前致谢!

解决方法

是.它会捕获RuntimeException但是如果在catch块中出现任何Exception,你必须再次捕获它.

我建议你进行本地部署并调试代码.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读