本文介绍了哪个更快,在java中尝试catch或if-else(WRT性能)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪一个更快:

这个

try {
  n.foo();
} 
catch(NullPointerException ex) {
}

if (n != null) n.foo();


推荐答案

这不是一个更快的问题,而是一个问题正确性。

It's not a question of which is faster, rather one of correctness.

例外情况恰恰相反,例外

如果 n 可能是 null 作为正常业务逻辑的一部分,那么使用 if..else ,否则抛出例外。

If it is possible for n to be null as part of normal business logic, then use an if..else, else throw an exception.

这篇关于哪个更快,在java中尝试catch或if-else(WRT性能)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:22