• Post Reply Bookmark Topic Watch Topic
  • New Topic

When does java stack trace show the offending line number?  RSS feed

 
Ben Ethridge
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all.

I notice that sometimes the stack trace shows a line number and sometimes it doesn't. (Sometimes it only shows the method name which generated the exception.

For example, while running, my code generated a NullPointerException, and the stack trace showed no line number where the exception occurred:

-------------------------------------------------------
java.lang.NullPointerException
at java.lang.Throwable.<init>(Throwable.java)
at java.lang.Throwable.<init>(Throwable.java:73)
at java.lang.NullPointerException.<init>(NullPointerException.java:60)
at java.math.BigDecimal.matchScale(BigDecimal.java:1787)
at java.math.BigDecimal.add(BigDecimal.java:496)
at com.republic.externalinterface.billingExport.TFGBillingExport.groupStmtItemPrintDOListBySortKey(TFGBillingExport.java)
at com.republic.externalinterface.billingExport.TFGBillingExport.transformStatementLevelElement(TFGBillingExport.java)
----------------------------------------------------------------------

I added a try-catch block around the code in the method, caught and re-threw NullPointerException, and now the printStackTrace shows the exact line number:

------------------------------------------------------------------------
java.lang.NullPointerException
at java.lang.Throwable.<init>(Throwable.java)
at java.lang.Throwable.<init>(Throwable.java)
at com.republic.externalinterface.billingExport.TFGBillingExport.groupStmtItemPrintDOListBySortKey(TFGBillingExport.java:545)
at com.republic.externalinterface.billingExport.TFGBillingExport.transformStatementLevelElement(TFGBillingExport.java)
-----------------------------------------------------------------------


I'm trying to tweak my code so that it ALWAYS shows a line number in the stack trace, since obviously, that's useful post-mortem information.

Anyone know what the general java rule is for this? I don't think I've evere seen it written anywhere before.

Ben Ethridge
SCJP 1.4
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When does java stack trace show the offending line number?

Typically, when it can.

Note that line number information must be in the compiled bytecodes, in order for it to appear in the stack trace.

By default, the compiler includes line number information in the compiled class files. If you were to compile with the -g:none switch, line number information would not be available.
 
Ben Ethridge
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose my question then becomes: Assuming one has the -g switch turned on (which I do), when can't it?

My current guess is that I have too many levels of "throw ups" in the call stack, i.e. "throws" in the method signatures up the call levels, instead of catches in the callers, and java can't compute the line number for some reason, but I don't have good clean evidence of this yet.

I'm hoping that the "when can't it" is documented somewhere at java.sun.com, so I could know what the rules are, instead of trying to figure out the rules by trial and error.

Ben
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not experienced in understanding the internal workings of the JVM.

After reading Section 4.7.8 The LineNumberTable Attribute of the JVM spec, it occurs to me that some of these values might come into play with determing the answer to your question.

Perhaps someone with more JVM knowledge is around...
 
Daniel Mayer
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect it might have something to do with the number and nature of optimizations the hotspot engine already has done. That is, if you run in pure interpreted mode, ou should always see the line numbers (provided you used the right compile time switch).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!