try catch块的成本是多少? [英] What is the cost of try catch blocks?

查看:160
本文介绍了try catch块的成本是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好多了:

 if (condition) {
  try {
   //something
  } catch(SomeEx ex) {}
 }

而不是:

 try {
   if (condition) {
     //something
   }
 } catch(SomeEx ex) {}

当我输入try block时,JVM实际上做了什么?

What actually JVM do when I enter try block ?

编辑:
我不想知道在第二个例子中总是进去尝试...请回答这个问题。

I don't want to know that in second example always go in to try... Please answer the question.

推荐答案

在运行时明智地执行,只要没有异常,尝试不会花费任何费用。一旦发生异常,它只会花费运行时间。在这种情况下,if评估会慢得多。

Execution wise at run time, as long as there is no exception, try does not cost you anything. It only costs run time as soon as an exception occurs. And in that situation it is much slower that an if evaluation.

在JVM规范中,您会看到在执行路径上没有生成额外的字节代码:
http://docs.oracle.com/javase/ specs / jvms / se7 / html / jvms-3.html #jvms-3.12

In the JVM spec, you see that there is no extra byte code generated on the execution path: http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-3.html#jvms-3.12

这篇关于try catch块的成本是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆