0

I'm curious how the following is possible.

catch (Exception e) {
    e.printStackTrace();
}

When I debug my code he jumps into the catch blog - so far so good - but than i check what kind of exception it is, and it is e = null?!

How is that even possible? I have an exception but the exception is null?

Greetings,

user85421
  • 28,957
  • 10
  • 64
  • 87
user2978538
  • 53
  • 1
  • 2
  • 8
  • Check this : – Areo Nov 14 '13 at 12:56
  • For some reason, the debugger doesn't yet have the exception initialized before the first statement in the catch block. If you add a statement before, or if you step into and then out, it should be initialized. – Zoltán Nov 14 '13 at 12:56
  • Where is your debug pointer, when you check the value? if it is on the line `catch (Exception e) {`then your debugger has not caught the value of `e` yet. Go to the next line and check again. – LuigiEdlCarno Nov 14 '13 at 12:57
  • And maybe your running debug session is out of sync with your current code. Try to clean and rebuild before running again – devnull69 Nov 14 '13 at 12:58
  • the debug pointer is in e.printstacktrace(); i tried to rebuild, still it does not work. – user2978538 Nov 14 '13 at 12:59
  • I'd say the class binary does not match your code, and you should rebuild. Classloader thinks "catch (Exception e) {" is line 67, but your debugger thinks "e.printStackTrace();" is line 67; – Glenn Lane Nov 14 '13 at 12:59
  • You mean build - rebuild? I've already done that. – user2978538 Nov 14 '13 at 13:02

1 Answers1

0

Probably it is a problem with the debugger or the virtual machine - the exception is lazy loaded, that is, the exception is not loaded until needed. As long as no method from the exception is called, its data is not loaded. When the first method is called, the virtual machine fills the exception fields.

You should see the exception after executing the printStackTrace or using the debugger to execute one method from that exception (e.g. Expression View in Eclipse).

user85421
  • 28,957
  • 10
  • 64
  • 87
  • I'm using Android Studio, any example what I should do there? – user2978538 Nov 14 '13 at 13:03
  • sorry, I only use Eclipse - but have seen something similar there: exception is __empty__ only after calling `printStackTrace` all fields are filled. What happens after executing the next line, I mean the `e.printStackTrace()`. Is it printed correctly? Can you see the exception in the debugger? – user85421 Nov 14 '13 at 13:06
  • well, my debug pointer is at e.printStackTrace - or what do you mean? – user2978538 Nov 14 '13 at 13:07
  • what happens after executing that line (single step once) – user85421 Nov 14 '13 at 13:11
  • It is the end of my code – user2978538 Nov 14 '13 at 13:15
  • normally the debuuger should stop after that line - but you can insert an additional (dummy) line, or just repeat the printStackTrace. Probably would be best to get help from someone using Android Studio. – user85421 Nov 14 '13 at 13:19
  • 1
    oh wow, adding an additional e.printStackTrace in the line after the first e.printStackTrace shows me the exception. Probably i don't have to understand this right? Well case and problem solved. – user2978538 Nov 14 '13 at 13:23