Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!news-out.readnews.com!news-xxxfer.readnews.com!postnews.google.com!news1.google.com!newsfeed2.dallas1.level3.net!news.level3.com!bos-service1.raytheon.com!bos-service2b.ext.ray.com.POSTED!53ab2750!not-for-mail Newsgroups: comp.lang.java.programmer Subject: Re: Where am I? From: Ian Shef References: Message-ID: User-Agent: Xnews/06.08.25 Lines: 38 Date: Thu, 13 Oct 2011 00:39:02 GMT NNTP-Posting-Host: 147.24.143.157 X-Complaints-To: news@ext.ray.com X-Trace: bos-service2b.ext.ray.com 1318466342 147.24.143.157 (Wed, 12 Oct 2011 19:39:02 CDT) NNTP-Posting-Date: Wed, 12 Oct 2011 19:39:02 CDT Organization: Raytheon Company Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8746 Roedy Green wrote in news:tmac97tlq14j1tk7tfmule337v9sc6q1rh@4ax.com: > It would be nice for debugging to include the line number of where the > code is when printing out the error message. Is there a simple way to > get it, or do you need to create a Throwable then analyse the > stacktrace? I don't know an easy way. I have a utility class with this method: public static String getCallerLineNumber() { StackTraceElement [] stArray = new Throwable().getStackTrace() ; StackTraceElement st ; if ((null!=stArray)&&(stArray.length>1)&&(null!=stArray[1])) { st = stArray[1] ; } else { st = UNAVAILABLE ; } return String.valueOf(st.getLineNumber()) ; } where I have defined: private static final StackTraceElement UNAVAILABLE = new StackTraceElement( "unavailable", "unavailable", "unavailable", -1) ; Notes: 1) The "if" statement may be excessive (especially null!=stArray ) but is intended to deal with the documented possibility that the stack trace is incomplete. 2) "Line number" is a hazy concept where optimization and JIT compiling is taking place. 3) This could be optimized, but it is intended for clarity and not for speed. Good Luck!