If you print the stack trace of an Exception, line numbers are included, so it would appear it is possible. Try looking at the source code for the Exception class to see how it is done there. The JDK installation includes the source code in a file called src.zip.
I will tell you a naughty way to do this. in a try block throw an exception. Then catch that there only. then parse the stack trace to get the line number. Oh now you got the line number. Do whatever you want ( +2 , -2 )
Originally posted by Murali Mohan: I will tell you a naughty way to do this. in a try block throw an exception. Then catch that there only. then parse the stack trace to get the line number. Oh now you got the line number. Do whatever you want ( +2 , -2 )
How is it?
You don't actually need to throw and catch the exception. Just create one and you can then retrieve the stack trace from it.
Note that in recent versions of Java, the exception object isn't needed. Just call Thread.currentThread().getStackTrace() to get an array of StackTraceElement objects.
Originally posted by Ernest Friedman-Hill: Note that in recent versions of Java, the exception object isn't needed. Just call Thread.currentThread().getStackTrace() to get an array of StackTraceElement objects.
I don't think so is it possible in JDK 1.4
The Best way to predict your future is to create it - Every great individual common man
Don't get me started about those stupid light bulbs.