gpt4 book ai didi

java - try catch会降低效率吗?

转载 作者:行者123 更新时间:2023-12-02 02:12:14 31 4
gpt4 key购买 nike

以下之间是否存在效率差异:-

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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com