简体   繁体   English

捕获异常时如何查看完整的堆栈跟踪

[英]How do I see the full stack trace when I catch an exception

My Java code has this (but leaving aside whether I should catch RuntimeException I have my reasons) 我的Java代码具有此功能(但我有理由不考虑是否应该捕获RuntimeException)

catch(RuntimeException e) {
    MainWindow.logger.log(Level.SEVERE, InfoMessage.MSG_MUSICBRAINZ_QUERY_SYNTAX_ERROR.getMsg(song.getRecNo(), song.getFile().getName()),e.getCause());
    stats.incUnableToMatchIds();
}

I don't understand why when an exception does occur it only goes as far as this catch block, so it doesn't pinpoint where the exception occurs. 我不明白为什么当发生异常时它只会延伸到此catch块,因此它无法查明异常发生的位置。 There is no difference whether I use e.getCause() or e 我使用e.getCause()还是e都没有区别

java.lang.NullPointerException
    at com.jthink.jaikoz.manipulate.UpdateGroupFromMusicBrainzIdsWorker.call(UpdateGroupFromMusicBrainzIdsWorker.java:234)
    at com.jthink.jaikoz.manipulate.UpdateGroupFromMusicBrainzIdsWorker.call(UpdateGroupFromMusicBrainzIdsWorker.java:42)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

if I remove this catch block it then shows me the full stacktrace which is what I want it to do, 如果我删除了这个catch块,那么它将向我显示完整的堆栈跟踪,这是我想要执行的操作,

java.lang.NullPointerException
    at com.jthink.jaikoz.manipulate.classical.MusicBrainzSoundtrackChecker.updateSoundtrack(MusicBrainzSoundtrackChecker.java:37)
    at com.jthink.jaikoz.manipulate.UpdateGroupFromMusicBrainzIdsWorker.call(UpdateGroupFromMusicBrainzIdsWorker.java:152)
    at com.jthink.jaikoz.manipulate.UpdateGroupFromMusicBrainzIdsWorker.call(UpdateGroupFromMusicBrainzIdsWorker.java:42)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

How do I get the com.jthink.jaikoz.manipulate.classical.MusicBrainzSoundtrackChecker.updateSoundtrack(MusicBrainzSoundtrackChecker.java:37) line part using the logger in catch? 如何使用catch中的记录器获取com.jthink.jaikoz.manipulate.classical.MusicBrainzSoundtrackChecker.updateSoundtrack(MusicBrainzSoundtrackChecker.java:37)行部分?

Edit 编辑

Perhaps as suggested it is my logger, since if I add in 也许是建议的,因为我添加了

catch(RuntimeException e) { e.printStackTrace(); catch(RuntimeException e){e.printStackTrace(); MainWindow.logger.log(Level.SEVERE, } MainWindow.logger.log(Level.SEVERE,}

I get the output ok with e.printStackTrace() 我通过e.printStackTrace()获得了正确的输出

java.lang.NullPointerException
    at com.jthink.jaikoz.manipulate.classical.MusicBrainzSoundtrackChecker.updateSoundtrack(MusicBrainzSoundtrackChecker.java:37)
    at com.jthink.jaikoz.manipulate.UpdateGroupFromMusicBrainzIdsWorker.call(UpdateGroupFromMusicBrainzIdsWorker.java:152)
    at com.jthink.jaikoz.manipulate.UpdateGroupFromMusicBrainzIdsWorker.call(UpdateGroupFromMusicBrainzIdsWorker.java:42)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
01/12/2016 11.18.53:com.jthink.jaikoz.manipulate.Analyser:waitForWorkers:WARNING: Worker:0:Exception:null
java.lang.NullPointerException
    at com.jthink.jaikoz.manipulate.UpdateGroupFromMusicBrainzIdsWorker.call(UpdateGroupFromMusicBrainzIdsWorker.java:235)
    at com.jthink.jaikoz.manipulate.UpdateGroupFromMusicBrainzIdsWorker.call(UpdateGroupFromMusicBrainzIdsWorker.java:42)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

So you have an NPE. 所以您有一个NPE。 In other words, you're calling a method on an object that has not been initialized (ie constructed). 换句话说,您正在对尚未初始化(即构造)的对象调用方法。
Sometimes, it can be tricky to solve, because the non-initialized object can be nested in a nested in a nested... object and the method call generating this exception can be very remote. 有时,解决起来可能很棘手,因为未初始化的对象可以嵌套在嵌套的nested ...对象中,并且生成此异常的方法调用可能非常遥远。

Here is a hypothesis: if you don't have the same stacktrace without the try / catch block, than maybe it's a different exception. 这是一个假设:如果没有try / catch块就没有相同的堆栈跟踪,那么可能是不同的例外。 What i mean is: maybe your catch block itself is generating an NPE from a different location than the one in the try block. 我的意思是:也许您的catch块本身从不同于try块中的位置生成NPE。 This is suggested by your logging line: 这是您的日志记录行建议的:

InfoMessage.MSG_MUSICBRAINZ_QUERY_SYNTAX_ERROR.getMsg(song.getRecNo(), song.getFile().getName()),e.getCause());

There is too much going on there! 那里发生了太多事情! Try debug this code, with and without try / catch , inspecting every object. 尝试调试此代码,无论是否使用try / catch ,都要检查每个对象。 I'm sure, you'll find a different NPE in each case. 我敢肯定,每种情况下您都会找到不同的NPE。

For more background, NPE should not happen in production code. 为了获得更多背景知识,NPE不应在生产代码中发生。 You should pay attention that all your objects (and sub-objects) are initialized. 您应该注意所有对象(和子对象)都已初始化。
Many recently developped languages forbid or find ways to prevent this error. 许多最近开发的语言禁止或找到防止此错误的方法。
Please see this reference: Tony Hoare , regretting the invention of null . 请参阅以下参考资料: Tony Hoare ,对null的发明感到遗憾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM