{"harness": "opencode", "name": "Try-Catch Blocks and Exception Handlers", "id": "lang-0030", "type": "session"} {"message": {"content": "You are an expert Java bytecode engineer specializing in ASM, bytecode manipulation, and class transformation.", "role": "system"}, "type": "message"} {"message": {"content": "How do I handle try-catch blocks in ASM? Show me visitTryCatchBlock and exception handlers.", "role": "user"}, "type": "message"} {"message": {"content": "Try-catch blocks in ASM use Label objects to define the protected region and handler entry point:\n\nimport org.objectweb.asm.*;\n\npublic class TryCatchExample {\n public static void main(String[] args) throws Exception {\n ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);\n cw.visit(Opcodes.V11, Opcodes.ACC_PUBLIC, \"Example\", null, \"java/lang/Object\", null);\n\n MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, \"safeDivide\", \"(II)I\", null, null);\n mv.visitCode();\n\n Label tryStart = new Label();\n Label tryEnd = new Label();\n Label handler = new Label();\n Label after = new Label();\n\n mv.visitLabel(tryStart);\n mv.visitVarInsn(Opcodes.ILOAD, 1);\n mv.visitVarInsn(Opcodes.ILOAD, 2);\n mv.visitInsn(Opcodes.IDIV);\n mv.visitInsn(Opcodes.IRETURN);\n mv.visitLabel(tryEnd);\n\n mv.visitLabel(handler);\n mv.visitVarInsn(Opcodes.ASTORE, 3);\n mv.visitFieldInsn(Opcodes.GETSTATIC, \"java/lang/System\", \"out\", \"Ljava/io/PrintStream;\");\n mv.visitVarInsn(Opcodes.ALOAD, 3);\n mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, \"java/lang/Throwable\", \"getMessage\", \"()Ljava/lang/String;\", false);\n mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, \"java/io/PrintStream\", \"println\", \"(Ljava/lang/String;)V\", false);\n mv.visitInsn(Opcodes.ICONST_0);\n mv.visitInsn(Opcodes.IRETURN);\n\n mv.visitLabel(after);\n mv.visitInsn(Opcodes.RETURN);\n\n mv.visitTryCatchBlock(tryStart, tryEnd, handler, \"java/lang/ArithmeticException\");\n mv.visitMaxs(2, 4);\n mv.visitEnd();\n cw.visitEnd();\n }\n}\n\nMultiple exception handlers with catch-all:\n\nLabel tryStart = new Label();\nLabel tryEnd = new Label();\nLabel npeHandler = new Label();\nLabel aioobHandler = new Label();\nLabel defaultHandler = new Label();\n\nmv.visitLabel(tryStart);\n// ... protected code ...\nmv.visitLabel(tryEnd);\n\nmv.visitLabel(npeHandler);\nmv.visitVarInsn(Opcodes.ASTORE, 1);\nmv.visitInsn(Opcodes.ICONST_0);\nmv.visitInsn(Opcodes.IRETURN);\n\nmv.visitLabel(aioobHandler);\nmv.visitVarInsn(Opcodes.ASTORE, 1);\nmv.visitInsn(Opcodes.ICONST_M1);\nmv.visitInsn(Opcodes.IRETURN);\n\nmv.visitLabel(defaultHandler);\nmv.visitVarInsn(Opcodes.ASTORE, 1);\nmv.visitInsn(Opcodes.ICONST_0);\nmv.visitInsn(Opcodes.IRETURN);\n\nmv.visitTryCatchBlock(tryStart, tryEnd, npeHandler, \"java/lang/NullPointerException\");\nmv.visitTryCatchBlock(tryStart, tryEnd, aioobHandler, \"java/lang/ArrayIndexOutOfBoundsException\");\nmv.visitTryCatchBlock(tryStart, tryEnd, defaultHandler, null);\n\nKey points:\n- visitTryCatchBlock(start, end, handler, type) registers the handler\n- type is the internal name of the exception class, or null for catch-all\n- Exception handlers receive the exception on the stack (store it with ASTORE)\n- Try-catch blocks can overlap and nest\n- The JVM tries handlers in order; put specific exceptions before catch-all\n- visitTryCatchBlock can appear anywhere in the method, but start/end/handler labels must be defined", "role": "assistant"}, "type": "message"}