且构网

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

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

更新时间:2023-01-15 16:52:47

通常RuntimeExceptions异常,可以通过编程来防止.例如 NullPointerExceptionArrayIndexOutOfBoundException.如果您在调用任何方法之前检查 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.