1

I'm doing instrumentation for java code with ASM library.

I want to surround a method body with a try-catch block, which doesn't interrupt the origianl method execution. However, when I use visitTryCatchBlock, a dominant handler is registered at the beginning of the method's exception table. Can I register the handler at the end of the method's exception table?

I couldn't really find examples of it online. Thank you.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847

1 Answers1

1

This has been a discussion on the ASM issue tracker but was rejected as a feature. The exception table needs to be appended in the order it is written to the method attributes in the order it is visited.

(...) it's one of those cases where the you know that you can do a better job than ASM that can not be easily fixed while remaining backward compatible, I'm against adding new flags for a corner cases given that there is a good chance of mis-using the API

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
  • Hi, thanks for your answer. I did need to visit the try-catch block in the order. I solved this by following your example: [Adding try/catch block in bytecode through ASM](https://stackoverflow.com/questions/23491902/adding-try-catch-block-in-bytecode-through-asm) and moving `visitTryCatchBlock(start, end, handler, "java/lang/Exception");` from `visitCode` to `visitMaxs`. Since `visitMaxs` is after `visitTryCatchBlock` – a stand-out flamingo Jan 26 '23 at 17:31