java.lang.RuntimeException和java.lang.Exception之间的区别 [英] Difference between java.lang.RuntimeException and java.lang.Exception

查看:122
本文介绍了java.lang.RuntimeException和java.lang.Exception之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人请解释一下 java.lang.RuntimeException java.lang.Exception 之间的区别?如果我创建自己的异常,如何决定扩展哪一个?

Someone please explain the difference between java.lang.RuntimeException and java.lang.Exception? How do I decide which one to extend if I create my own exception?

推荐答案

通常 RuntimeExceptions 是可以通过编程方式阻止的例外。 E.g NullPointerException ArrayIndexOutOfBoundException 。如果在调用任何方法之前检查 null ,则永远不会发生 NullPointerException 。类似地,如果首先检查索引,则永远不会发生 ArrayIndexOutOfBoundException 。编译器未检查 RuntimeException ,因此它是干净的代码。

Generally RuntimeExceptions are exceptions that can be prevented programmatically. E.g NullPointerException, ArrayIndexOutOfBoundException. If you check for null before calling any method, NullPointerException would never occur. Similarly ArrayIndexOutOfBoundException would never occur if you check the index first. RuntimeException are not checked by the compiler, so it is clean code.

编辑 :这些天人们喜欢 RuntimeException 因为它产生的干净代码。这完全是个人选择。

EDIT : These days people favor RuntimeException because the clean code it produces. It is totally a personal choice.

这篇关于java.lang.RuntimeException和java.lang.Exception之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆