更新时间:2023-01-15 16:52:47
通常RuntimeExceptions是异常,可以通过编程来防止.例如 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.
EDIT :现在人们喜欢 RuntimeException
因为它生成的代码很干净.这完全是个人选择.
EDIT : These days people favor RuntimeException
because the clean code it produces. It is totally a personal choice.