java - try catch会降低效率吗?

标签 java performance try-catch

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

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/

相关文章:

performance - 使用int64而不是int32时,for循环性能要慢得多

Java catch 被忽略

powershell:如何捕获invoke-sqlcmd引起的错误?

java - 如何从另一个数组中存在的值创建数组

data-structures - 无缓存数据结构和动态语言——有效吗?

java - 使用 Eclipse 文件资源管理器

php - 获取社交网络 php/mysql 好友操作的最佳方式

r - R rmongodb try()未捕获可能由错误数据引起的错误

java - Jetty:将 .war 解压到特定文件夹

java - Jersey 注释的注释处理器