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

本案例使用的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发现捕获得了 异常了!

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-05-02 05:08:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-02 05:08:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-02 05:08:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-02 05:08:04       20 阅读

热门阅读

  1. C++ 学习笔记

    2024-05-02 05:08:04       12 阅读
  2. Codeforces Round 942 (Div. 2) D2. Reverse Card (Hard Version)

    2024-05-02 05:08:04       11 阅读
  3. 算法:二叉树的所有路径

    2024-05-02 05:08:04       11 阅读
  4. NLP(11)--词向量

    2024-05-02 05:08:04       12 阅读
  5. LeetCode 刷题 -- Day 8

    2024-05-02 05:08:04       9 阅读