以下之间是否存在效率差异:-
public boolean canDivisionBeDone(int iA, int iB){
try{
float a = iA/iB;
}catch(Exception e){
return false;
}
return true;
}
和
public boolean canDivisionBeDone(int iA, int iB){
if(iB == 0){
return false;
}else{
float a = iA/iB;
}
return true;
}
如果是,为什么?
最佳答案
using try
本身没有任何开销,但如果 using block 创建了太多异常,您应该尝试检查您的代码。
Creating an exception in Java is a very slow operation. Expect that throwing an exception will cost you around 1-5 microseconds. Nearly all this time is spent on filling in the exception thread stack. The deeper the stack trace is, the more time it will take to populate it.
了解更多详细信息,请阅读here
关于java - try catch会降低效率吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49849991/