title: Embed the existing code of a method in a try-finally block (2)
body: <p>Some time ago, I asked in <a href="https://stackoverflow.com/questions/17291965/embed-the-existing-code-of-a-method-in-a-try-finally-block">Embed the existing code of a method in a try-finally block</a> how to wrap the body of a method in a try-finally block using ASM.
  The solution was to visit a label for the try block at the beginning of the method body in <code>visitCode()</code> and to complete the try-finally block when visiting an instruction with a return opcode in <code>visitInsn()</code>. I was aware that the solution won't be working if a method has no return instruction which applies if the method is always leaving with an exception.</p>
  
  <p>Though, I discovered that the former solution is sometimes inappropriate for methods with return instructions, too. It won't be working if a method has more than one return instruction. The reason is that it generates invalid bytecode because one try-finally block is added at the beginning of the method but more than one try-finally block is completed.</p>
  
  <p>Usually (but probably depending on the javac compiler), a bytecode method contains a single return instruction and all return paths end at that instruction by jumping there. However, the compilation of the following code with Eclipse will lead to byte code with two return instructions:</p>
  
  <pre><code>public boolean isEven(int x) {
    return x % 2 == 0;
  }
  </code></pre>
  
  <p>Byte code compiled with Eclipse:</p>
  
  <pre><code>   0: iload_1
     1: iconst_2
     2: irem
     3: ifne          8
     6: iconst_1
     7: ireturn       // javac compilation: goto 9
     8: iconst_0
     9: ireturn
  </code></pre>
  
  <p>Thus, I am wondering <strong>what the proper way to wrap the whole code of a method code is.</strong></p>