Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #8746

Re: Where am I?

Newsgroups comp.lang.java.programmer
Subject Re: Where am I?
From Ian Shef <invalid@avoiding.spam>
References <tmac97tlq14j1tk7tfmule337v9sc6q1rh@4ax.com>
Message-ID <Xns9F7CB38D9F968vaj4088ianshef@138.125.254.103> (permalink)
Date 2011-10-13 00:39 +0000
Organization Raytheon Company

Show all headers | View raw


Roedy Green <see_website@mindprod.com.invalid> 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!

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Where am I? Roedy Green <see_website@mindprod.com.invalid> - 2011-10-12 17:04 -0700
  Re: Where am I? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-10-12 17:22 -0700
    Re: Where am I? bugbear <bugbear@trim_papermule.co.uk_trim> - 2011-10-13 09:36 +0100
      Re: Where am I? Arne Vajhøj <arne@vajhoej.dk> - 2011-10-13 17:00 -0400
  Re: Where am I? Stanimir Stamenkov <s7an10@netscape.net> - 2011-10-13 03:23 +0300
    Re: Where am I? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-10-13 07:16 -0300
      Re: Where am I? Arne Vajhøj <arne@vajhoej.dk> - 2011-10-13 22:51 -0400
        Re: Where am I? Patricia Shanahan <pats@acm.org> - 2011-10-14 04:27 +0100
          Re: Where am I? Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-10-14 07:26 -0300
            Re: Where am I? Arne Vajhøj <arne@vajhoej.dk> - 2011-10-14 18:17 -0400
    Re: Where am I? Roedy Green <see_website@mindprod.com.invalid> - 2011-10-13 10:51 -0700
  Re: Where am I? Ian Shef <invalid@avoiding.spam> - 2011-10-13 00:39 +0000
  Re: Where am I? markspace <-@.> - 2011-10-12 18:00 -0700

csiph-web