java 捕获运行时异常_java-如何通过ASM在字节码中捕获运行时异常

我试图通过异常捕获运行时异常.我能够捕获通常的方法退出事件.

但是,控件永远不会到达opcode == Opcodes.ATHROW.

我认为在调用事件时做错了事.

这是我的示例代码:

public void visitCode() {

// mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");

// mv.visitLdcInsn("Entering method " + fQMethodName);

// mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream","println","(Ljava/lang/String;)V");

}

@Override

public void visitInsn(int opcode)

{

if (opcode == Opcodes.ATHROW)

{

mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");

mv.visitLdcInsn("Exiting on exception " );

mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",

"(Ljava/lang/String;)V");

}

super.visitInsn(opcode);

}

public void visitMethodInsn(int opcode, String owner, String name,

String desc) {

super.visitMethodInsn(opcode, owner, name, desc);

//

if (opcode == Opcodes.ATHROW)

{

mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;");

mv.visitLdcInsn("Exiting on exception " + name);

mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",

"(Ljava/lang/String;)V");

}

else if (!name.equals("println")

&& !name.equals("")

&& (opcode == Opcodes.INVOKEVIRTUAL || opcode == Opcodes.INVOKESPECIAL

|| opcode == Opcodes.INVOKESTATIC || opcode == Opcodes.INVOKEDYNAMIC)) {

this.currentMethod = name;

onFinally(opcode);

}

}

private void onFinally(int opcode) {

mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "err",

"Ljava/io/PrintStream;");

mv.visitLdcInsn("Returning to " + fQMethodName + " from " + currentMethod);

mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println",

"(Ljava/lang/String;)V");

}

在运行时异常时,将清除现有堆栈内容,仅保留可抛出对象.我可以通过ASM获得堆栈顶部元素(基本上是堆栈框架)吗?

非常感谢.

编辑:

这是我添加try-catch块后的修改程序.

@Holger:谢谢您的帮助.真的很感激.

因此,我试图在ASM的MethodVisitor中插入动态try-catch块.

这是我的代码:

public void visitCode()

{

super.visitCode();

this.visitTryCatchBlock(lblTryBlockStart, lblTryBlockEnd, lblCatchExceptionBlockStart, "java/lang/Exception");

this.visitLabel(lblTryBlockStart);

}

public void visitMaxs(int maxStack, int maxLocals)

{

// visit try block end label

this.visitLabel(lblTryBlockEnd);

// visit normal execution exit block

this.visitJumpInsn(Opcodes.GOTO, exitBlock);

// visit catch exception block

this.visitLabel(lblCatchExceptionBlockStart);

// store the exception

this.visitVarInsn(Opcodes.ASTORE, 1);

// load the exception

this.visitVarInsn(Opcodes.ALOAD, 1);

// call printStackTrace()

//this.visitInsn(Opcodes.ATHROW);

this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Exception", "printStackTrace", "()V");

// exit from this dynamic block

this.visitLabel(exitBlock);

super.visitMaxs(maxStack+2, maxLocals+2);

}

我的意图是添加动态try-catch块,如果抛出任何异常,则将其打印出来.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值