且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

java.lang.RuntimeException和java.lang.Exception之间的区别

更新时间:2023-01-15 16:39:21

通常 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.