What is the cost of catch catch blocks?

How much better:

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

instead of this:

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

What does the JVM actually do when entering a try block?

EDIT: I don't want to know that in the second example you always need to try ... Please answer the question.

+6
source share
6 answers

Doing the wise at run time, if there is no exception, trying does not cost you anything. This is only related to runtime as soon as an exception occurs. And in this situation, the if evaluation is much slower.

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

+7
source
 try {if (condition) {...}} catch(SomeEx ex) {} 

Here you have thrown an exception if it is a condition, if it also occurred inside an if-block .

 if (condition) {try {...} catch(SomeEx ex) {}} 

This is where you handle the exception if it occurs only inside the if-block . If something goes wrong in the if condition, then it will not be processed.

So it depends on the actual senario.

+3
source

In terms of performance, this should be the same. Throwing an exception is an expensive operation (first you need to create and populate a trace stack). The mere existence of a try block has (or slightly) a performance penalty.

See If java try blocks are blocked as tightly as possible .

What does the JVM actually do when entering a try block?

From JLS 14.20.1. Executing try-catch :

A try statement without a finally block is executed by the first execution of the try block. Then there is a choice:

  • If the try block completes normally, no further action is taken, and the try statement completes normally.

  • If the execution of the try block terminates abruptly due to the throwing of the value of V, then there is a choice:

    • If the run-time type V is an assignment compatible with (ยง5.2), the subtle exception class of any catch clause from the try statement, then the first (far left) catch clause is selected. The value V is assigned to the parameter of the selected catch clause, and the block of this catch condition is executed, and then there is a choice:

      • If this block completes normally, then the try statement completes normally.

      • If this block terminates unexpectedly for some reason, then the try statement terminates abruptly for the same reason.

    • If the run-time type of V is not an assignment compatible with the subtle exception class of any catch clause in the try statement, then the try statement terminates abruptly by throwing the value of V.

  • If a try block completes abruptly for any other reason, then a try statement terminates abruptly for the same reason.

EDIT:

For a detailed description of the exception, see the JVM 2.10 link in the "New Idiot" response .

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

better to use because it executes a try block if the condition is approved. The JVM compiles the try block and checks the catch block or finally block. I think the advantage is not at compile time, but at runtime. Compilation time I think no advantage at all

0
source

Exceptions should be an exceptional case, and not every time the code is executed. Therefore, it is better to check the condition before try ing!

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

Make sure that if (condition) does not throw an Exception .

It depends on your use and functionality. For example, this would be better:

 if (someObject!=null) { try { someObject.getSomething(); // getSomething() potentially throws some Exception } catch(SomeEx ex) {} } 

What does the JVM actually do when entering a try block?

Read the JVM spec 2.10 .

0
source

If you see oracle documents

 >try { code } catch and finally blocks . . . 

A segment in the corresponding code example contains one or more legal lines of code that can throw an exception.

So, if you doubt your If condition, that it will throw a exception put it inside.

0
source

Source: https://habr.com/ru/post/950702/


All Articles