I have seen code with sysout statements to debug like Error 1, Error 2. This is good idea to debug the code easily instead of checking log files. I was wondering if we can get the line number of java code that would be great to debug.
public class TestLineNumber {
public static void main(String[] args){
int x = 10;
System.out.println("X value is "+ x
+ " at line number "+ new Exception().getStackTrace()[0].getLineNumber());
x++;
System.out.println("X value is "+ x
+ " at line number " + new Exception().getStackTrace()[0].getLineNumber());
x++;
System.out.println("X value is "+ x
+ " at line number " + new Exception().getStackTrace()[0].getLineNumber());
}
}
Output will look like below:
X value is 10 at line number 6
X value is 11 at line number 9
X value is 12 at line number 12
Friday, October 12, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment