如何报告JVM中的所有异常,无论是自己还是第三方代码? [英] How to report all exceptions in JVM, be it own or third-party code?

查看:110
本文介绍了如何报告JVM中的所有异常,无论是自己还是第三方代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以记录在JVM中发生的所有异常?

系统是由大量较小的组件不同的团体。它们都在同一个JVM中运行(在Weblogic下)。

错误报告政策在这些群组中非常不同,所以有时我们会遇到抑制性异常情况,这使得排除故障非常困难。同时,我注意到JDB能够拦截代码中发生的任何异常,无论在哪里。



我想使用JDB使用的相同工具在DEV / SIT / UAT环境中缩短问题解决时间。在PTE和PROD中,虽然设施不仅应该关闭,而且不要影响性能
$ b



  • 不需要更改代码以关闭它。



当然,我可以将JDB连接到在DEV中运行的每个服务器实例/坐。这是可行的,我猜...但有两个主要缺点:$ b​​
$ b $ ul

  • 它使配置更复杂

  • 发生异常时JDB停止;需要一个脚本或其他东西让它继续



  • 所以我想知道是否有任何方法可以做到这一点,例如Runtime.traceExceptions(...)?

    解决方案

    据我所知,没有官方的Java API这除了调试API以外,还可以在将被调试的虚拟机内部使用。



    一种更简单快捷的方法来获得每个异常的通知在运行时创建的是将一些跟踪代码直接插入到Throwable类的构造函数中,编译并将它放入rt.jar之前的 bootclasspath 中。这当然不是你应该在发布版本中做的,但是它可以非常有用地分析捕获异常的代码,并且不会正确地报告它们。

    >回复评论:

    关于Debugger API:上次我做了这样的事情时,我使用了 JVMDI 构建本地JNI / JVMDI dll并访问它从VM内部。它可以工作,并且可以执行Java通常不提供的各种有线事物,但我认为这比使用修补的Throwable类更像是黑客攻击。除此之外,JVMDI已被弃用,并自Java 1.6以后被JVM TI所取代。我不知道是否以及如何使用这个新的Debugger API来做类似的事情。



    使用修改后的Throwable很快,很容易找到正确的,doesn如果你控制着执行环境,它本身并不会造成任何性能下降,甚至不会真正发生黑客行为。你可以把它想象成某种AOP。 ;)但是,我仍然只会用它来发现其他难以察觉的错误,而不是在生产代码中或仅作为最后的手段。像这样的东西肯定不应该被视为设计的一部分。最好的长期方法是让所有的开发小组同意一些常见的异常处理。

    net /rel =nofollow noreferrer> FindBugs 可以提供很多帮助,如果您决定进行代码审查以找到所有这些发现异常被捕获和未报告的点。


    Is there a way to log all exceptions happening within JVM?

    A system is built from a big number of smaller components provided by different groups. All of them are running within the same JVM (under Weblogic).

    Error reporting policies are rather different in those groups, so sometimes we have cases of suppressed exceptions, which makes troubleshooting very hard. At the same time, I noticed that JDB is able to intercept any exception happening in the code, no matter where.

    I'd like to use the same facility JDB uses in DEV/SIT/UAT environment to shorten issue resolution time. In PTE and PROD though the facility shall not only be turned off, but

    • create no performance impact
    • require no code change for turning it off.

    Of course, I can have JDB connected to every instance of server running in DEV/SIT. It's feasible, I guess... but there are two main downside:

    • it makes configuration more complex
    • JDB stops when an exception has occured; need a script or something to let it continue

    So I wonder is there any method that does it, e.g. Runtime.traceExceptions(...)?

    解决方案

    As far as I know there is no official Java API for this besides the debugging API which could be used also from inside the VM that is about to be 'debugged'.

    An easier quick and dirty way to get notified of every exception that is created at runtime is to hack some tracing code directly into the constructor(s) of the Throwable class, compile it and put it into the bootclasspath before the rt.jar. This is of course nothing you should do in a release version but it can be very helpfull to analyse code that catches exceptions somewhere and doesn't report them properly.

    In response to the comments:

    Regarding the Debugger API: The last time I did something like this I used the JVMDI to build a native JNI/JVMDI dll and accessed it from inside the VM. It works and lets you do all sorts of wired things that Java normally doesn't offer but I would consider this even more of a hack than using a patched Throwable class. Besides that, the JVMDI has been deprecated and was replaced with JVM TI since Java 1.6. I don't know if and how you can do something like this with this new Debugger API as well.

    Using a modified Throwable is fast, easy to get right, doesn't impose any performance degeneration by itself and also not even really hackish if you are in control over the execution environment. You could think of as some kind of AOP. ;) But I would still only use it to find otherwise hard to detect bugs and not in production code or only as a last resort. Something like this should surely not be considered as part of the design. The best long term approach would be to get all your development groups to agree on some common exception treatment.

    Also FindBugs can help a lot if you decide to do a code review to find all these spots where exceptions get caught and not reported.

    这篇关于如何报告JVM中的所有异常,无论是自己还是第三方代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆