arthas无法捕获到try catch了的异常怎么办呢?

8 篇文章 0 订阅
2 篇文章 0 订阅

本案例使用的arthas是最新版本3.7.2

要跟踪的代码:

1、arthas watch试下能不能捕获到

页面上请求 http://localhost:8080/exception发现捕获不了。

2、可以使用btrace捕获,能够捕获到

我本案例使用Eclipse编写btrace脚本 ,首先引入btrace的jar包

		<dependency> 
		    <groupId>com.sun.btrace</groupId> 
		    <artifactId>btrace-agent</artifactId> 
		    <version>1.3.11</version> 
		    <type>jar</type> 
		    <scope>system</scope> 
		    <systemPath>E:\java Tools\btrace-bin-1.3.10.2\build\btrace-agent.jar</systemPath> 
		</dependency>
		<dependency> 
		    <groupId>com.sun.btrace</groupId> 
		    <artifactId>btrace-boot</artifactId> 
		    <version>1.3.11</version> 
		    <type>jar</type> 
		    <scope>system</scope> 
		    <systemPath>E:\java Tools\btrace-bin-1.3.10.2\build\btrace-boot.jar</systemPath> 
		</dependency>
		<dependency> 
		    <groupId>com.sun.btrace</groupId> 
		    <artifactId>btrace-client</artifactId> 
		    <version>1.3.11</version> 
		    <type>jar</type> 
		    <scope>system</scope> 
		    <systemPath>E:\java Tools\btrace-bin-1.3.10.2\build\btrace-client.jar</systemPath> 
		</dependency>


1)编写btrace脚本代码

PrintOnThrow.java

package com.codex.terry.btrace;

import com.sun.btrace.BTraceUtils;
import com.sun.btrace.annotations.BTrace;
import com.sun.btrace.annotations.Kind;
import com.sun.btrace.annotations.Location;
import com.sun.btrace.annotations.OnMethod;
import com.sun.btrace.annotations.Self;
import com.sun.btrace.annotations.TLS;

/**
 * @编写人: yh.zeng
 * @编写时间:2024年5月1日 下午2:34:48
 * @文件描述: todo
 */

@BTrace 
public class PrintOnThrow {    
    // store current exception in a thread local
    // variable (@TLS annotation). Note that we can't
    // store it in a global variable!
    @TLS 
    static Throwable currentException;

    // introduce probe into every constructor of java.lang.Throwable
    // class and store "this" in the thread local variable.
    @OnMethod(
        clazz="java.lang.Throwable",
        method="<init>"
    )
    public static void onthrow(@Self Throwable self) {//new Throwable()
        currentException = self;
    }

    @OnMethod(
        clazz="java.lang.Throwable",
        method="<init>"
    )
    public static void onthrow1(@Self Throwable self, String s) {//new Throwable(String msg)
        currentException = self;
    }

    @OnMethod(
        clazz="java.lang.Throwable",
        method="<init>"
    )
    public static void onthrow1(@Self Throwable self, String s, Throwable cause) {//new Throwable(String msg, Throwable cause)
        currentException = self;
    }

    @OnMethod(
        clazz="java.lang.Throwable",
        method="<init>"
    )
    public static void onthrow2(@Self Throwable self, Throwable cause) {//new Throwable(Throwable cause)
        currentException = self;
    }

    // when any constructor of java.lang.Throwable returns
    // print the currentException's stack trace.
    @OnMethod(
        clazz="java.lang.Throwable",
        method="<init>",
        location=@Location(Kind.RETURN)
    )
    public static void onthrowreturn() {
        if (currentException != null) {
        	BTraceUtils.Threads.jstack(currentException);
        	BTraceUtils.println("=====================");
            currentException = null;
        }
    }
}

2)使用jps获取进程ID

2)执行btrace命令

btrace 1260 PrintOnThrow.java

页面上请求 http://localhost:8080/exception发现捕获得了 异常了!

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值