为什么我的NullPointerException没有被捕获在catch块中? [英] Why is my NullPointerException not being caught in my catch block?
问题描述
我有一个线程,可以在一个大的,包罗万象的catch块中捕获所有错误。我这样做是为了报告应用程序中的任何错误,而不仅仅是预期的错误。我的Runnable看起来像这样:
I have a thread in which I catch all errors in a big, all-encompassing catch block. I do this so that I can report any error, not just expected ones, in my application. My Runnable looks like this:
public final void run()
{
try
{
System.out.println("Do things"); /* [1] */
doUnsafeThings();
}
catch (Throwable t)
{
System.out.println("Catch"); /* [2] */
recover();
}
finally
{
System.out.println("Finally"); /* [3] */
}
}
我希望NPE被Throwable捕获块捕获。而是不打印[2]的输出,也不打印[3]。输出[1]的输出。
I would expect the NPE to be caught by the Throwable catch block. Instead, the output at [2] is not printed, and neither is [3]. The output at [1] is printed.
我在控制台上得到的是:
What I do get on the console, is this:
Uncaught exception java/lang/NullPointerException.
这到底是怎么回事?
根据法庭记录,我使用的是J2ME,它正在Sun的WTK v2.5.2模拟器中运行。
For the court records, I'm using J2ME, and this is running in Sun's WTK v2.5.2 emulator.
我很想将其归为JVM实现的可疑性,但我忍不住感觉自己只是缺少了一些东西。
I'm tempted to put it down to JVM implementation dodginess but I can't help feeling that I'm just missing something.
为避免疑问进行澄清(由于示例代码显然已从我的生产中更改而来代码)
To clarify for the avoidance of doubt (Since the example code is obviously altered from my production code)
- run方法中try / catch / finally块之外没有任何内容。
- 在每个块的开始处都有一个System.out.println-遵循那些控制台语句应该没关系。
推荐答案
答案表明我是个白痴。我会解释出了什么问题,但我们只能将其称为这些错误之一。
The answer turns out that I'm an idiot. I'd explain what went wrong, but let's just call it "one of those bugs".
我暂时忘记了运行runnable的线程是自定义线程类(解决一些诺基亚错误)。在调用 canWait()
方法之间,它反复调用 run()
。
I had momentarily forgotten that the thread that ran the runnable was a custom thread class (To get round some Nokia bugs). It called run()
repeatedly between calls to a canWait()
method.
canWait方法是导致失败的原因,而运行完全没有失败。最重要的是,我有点盲目性,完全但不小心将问题中的事件序列错误地引用了。
The canWait method was responsible for the failure, and run wasn't failing at all. To top it off, I have console-blindness and completely but accidentally misquoted the sequence of events in my question.
这篇关于为什么我的NullPointerException没有被捕获在catch块中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!