Java 如何使用asm实现try-catch?

Java 如何使用asm实现try-catch?,java,Java,我是asm新手,现在我想捕捉类中每个方法的异常,尝试自己实现并通过google搜索,但失败了,谁能为我写一个完整的示例 MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); mv.visitCode(); Label lTryBlockStart = new Label(); Label lTry

我是asm新手,现在我想捕捉类中每个方法的异常,尝试自己实现并通过google搜索,但失败了,谁能为我写一个完整的示例

       MethodVisitor mv = super.visitMethod(access, name, desc, signature,
                exceptions);
        mv.visitCode();
        Label lTryBlockStart = new Label();
        Label lTryBlockEnd = new Label();
        Label lCatchBlockStart = new Label();
        Label lCatchBlockEnd = new Label();
        // set up try-catch block for RuntimeException
        mv.visitTryCatchBlock(lTryBlockStart, lTryBlockEnd, lCatchBlockStart,
                "java/lang/Exception");
        mv.visitLabel(lTryBlockStart);
        // code to call the method goes here
        // mv.visitVarInsn(ALOAD, 0);
        // mv.visitMethodInsn(Opcodes.INVOKESPECIAL, this.name, name, desc);

        mv.visitLabel(lTryBlockEnd);
        mv.visitJumpInsn(GOTO, lCatchBlockEnd); // when here, no exception was
                                                // thrown, so skip exception
                                                // handler
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitFieldInsn(Opcodes.INVOKESPECIAL, this.name, name, desc);
        // exception handler starts here, with RuntimeException stored on the
        // stack
        mv.visitLabel(lCatchBlockStart);
        mv.visitVarInsn(ASTORE, 1); // store the RuntimeException in local
                                    // variable 1
        // here we could for example do e.printStackTrace()
        mv.visitVarInsn(ALOAD, 1); // load it
        mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Exception",
                "printStackTrace", "()V");

        // exception handler ends here:
        mv.visitLabel(lCatchBlockEnd);
        mv.visitEnd();`

我无法运行这些代码,它每次抛出一个奇怪的异常

“它抛出一个奇怪的异常”。你能说得更具体一点吗?java.lang.ClassFormatError:com/duang/framework/aop/ServiceTest_tmp类中的字段“save”有非法签名“()V”,“save”是该类中的一个方法,但它显示“save”是一个字段,我不明白。你的代码中不知道它在哪里被调用/使用posted@z10030313335, “谁能为我写一个完整的例子?”什么的例子?你想要简单的try-catch块吗?例如,try{your code here}catch(Exception e){e.printStackTrace()}