Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does JRE know the line number of code where exception occured?

Tags:

java

Consider the following Exception print

java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenizer.java:332)
    at com.infoaxe.mr.homefeed.ReduceTwo.reduce(MapReduce.java:290)

Since Java is a compiled language and what runs in JVM is the bytecode and not the source code itself how does the exception know on which line it occurred ? Example line 332 in above case ?

like image 676
Akshar Prabhudesai Avatar asked Apr 17 '13 20:04

Akshar Prabhudesai


1 Answers

Simply because the compiler includes the line numbers in the generated byte-code. There are -g options (in the Oracle javac compiler) allowing to turn that off, if you want to.

like image 64
JB Nizet Avatar answered Oct 23 '22 11:10

JB Nizet