Topic
2 replies Latest Post - ‏2011-06-01T00:31:42Z by SystemAdmin
SystemAdmin
SystemAdmin
9224 Posts
ACCEPTED ANSWER

Pinned topic getting the line number where a groovy exception happened?

‏2011-04-19T16:16:06Z |
NOTE: this thread was migrated from projectzero.org. Some content and formatting may have been lost in the move.


Originally posted by projectzero userid: barny - f=4&t=2304#p9358
When I get an exception I usually log it like this:
System.out.println( "Exception $e" )
but that doesn't print the line number where it happened.

I can use e.printStackTrace() but that is horribly long and hard to read - there must be a simpler way, surely?

How can I get the file and line number shown in a user-friendly way?
Updated on 2011-06-01T00:31:42Z at 2011-06-01T00:31:42Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    9224 Posts
    ACCEPTED ANSWER

    Re: getting the line number where a groovy exception happened?

    ‏2011-04-21T10:26:08Z  in response to SystemAdmin

    Originally posted by projectzero userid: EricWang - f=4&t=2304#p9363
    Java use Thread.currentThread().getStackTrace()[2].getLineNumber() to get the line number.
    Not test on Groovy.
  • SystemAdmin
    SystemAdmin
    9224 Posts
    ACCEPTED ANSWER

    Re: getting the line number where a groovy exception happened?

    ‏2011-06-01T00:31:42Z  in response to SystemAdmin

    Originally posted by projectzero userid: barny - f=4&t=2304#p9399
    After some painful digging, and a fair bit of wondering why on earth I have to code this/why it isn't built in to Groovy, this is what I now use:

    try {
    ....
    } catch (Exception e) {
    // this text is returned in the current web page
    println "Exception in (file/function):";
    println e;
    // this text is printed in the system trace, visible in the appbuilder Console and also in the error logs
    System.out.println( "Exception in (file/function): $e" )
    System.err.println( "Exception in (file/function): $e" )
    StackTraceElement[] stackTrace = e.getStackTrace();
    for (int i = 0; i < stackTrace.length; i++) {<br />
    StackTraceElement element = stackTrace[i];
    String fileName = element.getFileName();
    if (fileName!=null && !fileName.endsWith(".java")) {
    System.err.println("\tat " + element);
    System.out.println("\tat " + element);
    }
    }
    }