Line numbers in stack trace

M

Manish

Hi,
One thing that has always interested me is that whenever the stack
trace is printed when an exception occurs, the line number(s) of the
line(s) which caused the exception are also printed.
For example the following code :

class test
{
public static void main (String [] args)
{
int i = 42 / 0 ;
System.out.println(i) ;
}
}

generates the following trace :

Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.main(test.java:5)

Is there any article/post which explains how such a feature is
implemented in java ? If this question has already been answered could
someone please direct me to the correct page ?
 
A

Arved Sandstrom

Manish said:
Hi,
One thing that has always interested me is that whenever the stack
trace is printed when an exception occurs, the line number(s) of the
line(s) which caused the exception are also printed.
For example the following code :

class test
{
public static void main (String [] args)
{
int i = 42 / 0 ;
System.out.println(i) ;
}
}

generates the following trace :

Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.main(test.java:5)

Is there any article/post which explains how such a feature is
implemented in java ? If this question has already been answered could
someone please direct me to the correct page ?

The JVM spec will give you this info. After all, the line number information
(if you see it - you may not if the JIT compiler has already kicked in) will
be in the class file if at all. For example:

http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#22856

AHS
 
L

Lew

Manish wrote ...
One thing that has always interested me is that whenever the stack
trace is printed when an exception occurs, the line number(s) of the
line(s) which caused the exception are also printed. ....
Is there any article/post which explains how such a feature is
implemented in java [sic]? If this question has already been answered could
someone please direct me to the correct page ?

Arved said:
The JVM spec will give you this info. After all, the line number information
(if you see it - you may not if the JIT compiler has already kicked in) will
be in the class file if at all. For example:

http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#22856

There is also the Java tools documentation, which for javac tells us of the
'-g' option:
-g:{keyword list}
Generate only some kinds of debugging information,
specified by a comma separated list of keywords.
Valid keywords are:
source
Source file debugging information
lines
Line number debugging information
vars
Local variable debugging information

From this we see that such information is put in the class file somehow, to
the how of which Arved has already pointed us.

As for JIT/Hotspot, I suspect that an exception might cause the JVM to look at
the bytecode before reporting the problem, thus recovering any debug
information even after optimization. Also, IIRC, '-g' suppresses certain
optimizations, though I might be wrong about that.
 
J

Joshua Cranmer

Manish said:
Is there any article/post which explains how such a feature is
implemented in java ? If this question has already been answered could
someone please direct me to the correct page ?

The feature is implemented as one would expect: there exists in the
class file a mapping of bytecode offsets to line numbers, as well as the
original base filename. javac will output this filename information
unless you tell it to not do so via -g:none.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top