Why is my NullPointerException not being caught in my catch block?

风格不统一 提交于 2019-12-05 16:15:25
izb

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".

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.

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.

Sounds like you'll need some trial and error. May I suggest:

try {
    doEvilStuff();
} catch (NullPointerException ex) { 
    System.out.println("NPE encountered in body"); 
} catch (Throwable ex) {
    System.out.println("Regular Throwable: " + ex.getMessage());
} finally {
    etc...
}

By having an explicit catch for NullPointerException, it should become obvious if the exception is from within the try block or a catch/finally block.

Okay, this is a wild guess... but it would explain things.

Obviously your code isn't actually that - so my guess is that your catch (or finally) block is either doing something before it logs anything, or it uses a different logger than the try block. Either way, I suspect that either the catch or the finally block is throwing the exception.

I don't suppose you have a stack trace...

EDIT: Okay, if it's just System.out.println, is it something in the argument that might go bang? For example:

catch (Throwable t) {
    // Will go bang if t.getCause() returns null
    System.out.println(t.getCause().getMessage());
}

If it's just simple System.out.println("Constant") then it's very weird.

Do you know (e.g. from log lines within the try block) how far the try block is actually getting?

When I looked at your code it seems that recover() is throwing an exception, so the advice given by Jon would be excellent to follow.

If you gave us a stack trace you may get better help.

When I try to catch exceptions I do something like this:

try {
  doSomethingBad();
} catch(Exception e) {
   try {
      LogException(...);
   } catch(Exception e) {}       
} finally {
}

I don't like to nest exceptions, but I don't like my catch block throwing exceptions.

As you mention you are using a Runnable - does this by any chance mean that you are using multiple threads as well? If the doUnsafeThings() method internally spawns a different thread again and that produces the exception, you might not get it in the thread your catch block is. See http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.UncaughtExceptionHandler.html

It is generally a bad practice to catch NullPointerException.

Programmers typically catch NullPointerException under three circumstances:

The program contains a null pointer dereference. Catching the resulting exception was easier than fixing the underlying problem.
The program explicitly throws a NullPointerException to signal an error condition.
The code is part of a test harness that supplies unexpected input to the classes under test. 

Of these three circumstances, only the last is acceptable. following this link:

Catch NullPointerException

Is it possible that the thread is being killed by some other code? In general a finally block always executes unless the thread is abnormally terminated, either by System.exit() or something similar.

  • Are you sure you are looking at the right place in the code? I.e., is the doUnsafeThings() block you are protecting in of the stack trace?

  • Maybe there is a problem with your build method, and you are debugging an old version of the code?

just add some logging in the doUnsafeThings(); to see if that method is doing what you expect (e.g put a try catch finally and log something)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!