In C/C++ the filename is returned by FILE and line number is returned by LINE. Java does have a getFileName(), but does not seem to have a corresponding getLineNumber(). It would be nice to be able to do something like this:
catch (Exception e) {
System.err.println(this.getFileName() + this.getLineNumber() + e.getMessage());
}
Is there a way to get the java file/line number?
public static void main(String[] args)
{
StackTraceElement frame = new Exception().getStackTrace()[0];
System.out.println(frame.getFileName());
System.out.println(frame.getLineNumber());
}
Use this:
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/StackTraceElement.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With