472,122 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

How to get line no Exception occurred at?

I need to get hold of somehow the line no that an exception occurred at so
as to log it using log4j.

My ConversionPattern is set to -

log4j.appender.stdout.layout.ConversionPattern=%5p (%F:%L) - %m%n

but %L only shows the line no that log was called from.

Thought I'd cracked it when I noticed getStackTrace() method of exception
object BUT (notice the big but!), the app must work on 1.3 & getStackTrace()
wasn't introduced until 1.4.

Can I still somehow get hold of the StackTrace & retrieve the line no?

Thanks

harry

Jul 17 '05 #1
3 14985
Try using a PrintWriter, like this:

try {

...

} catch (Exception exception) {
StringWriter stringWriter = new StringWriter();
String stackTrace = null;
exception.printStackTrace(new PrintWriter(stringWriter));

// get the stackTrace as String...
stackTrace = stringWriter.toString();

The line no is usually the found in line 2 of the stacktrace after "java:"
greets
Rolf
Harry wrote:
I need to get hold of somehow the line no that an exception occurred at so
as to log it using log4j.

My ConversionPattern is set to -

log4j.appender.stdout.layout.ConversionPattern=%5p (%F:%L) - %m%n

but %L only shows the line no that log was called from.

Thought I'd cracked it when I noticed getStackTrace() method of exception
object BUT (notice the big but!), the app must work on 1.3 & getStackTrace()
wasn't introduced until 1.4.

Can I still somehow get hold of the StackTrace & retrieve the line no?

Thanks

harry




Jul 17 '05 #2
Cheers Rolf!

"Rolf Rosenbaum" <me@home.org> wrote in message
news:7A*********************@news.easynews.com...
Try using a PrintWriter, like this:

try {

...

} catch (Exception exception) {
StringWriter stringWriter = new StringWriter();
String stackTrace = null;
exception.printStackTrace(new PrintWriter(stringWriter));

// get the stackTrace as String...
stackTrace = stringWriter.toString();

The line no is usually the found in line 2 of the stacktrace after "java:"
greets
Rolf
Harry wrote:
I need to get hold of somehow the line no that an exception occurred at so as to log it using log4j.

My ConversionPattern is set to -

log4j.appender.stdout.layout.ConversionPattern=%5p (%F:%L) - %m%n

but %L only shows the line no that log was called from.

Thought I'd cracked it when I noticed getStackTrace() method of exception object BUT (notice the big but!), the app must work on 1.3 & getStackTrace() wasn't introduced until 1.4.

Can I still somehow get hold of the StackTrace & retrieve the line no?

Thanks

harry



Jul 17 '05 #3
Here is a second example as well :

http://www.javapractices.com/Topic78.cjp

Harry wrote:
I need to get hold of somehow the line no that an exception occurred at so
as to log it using log4j.

My ConversionPattern is set to -

log4j.appender.stdout.layout.ConversionPattern=%5 p (%F:%L) - %m%n

but %L only shows the line no that log was called from.

Thought I'd cracked it when I noticed getStackTrace() method of exception
object BUT (notice the big but!), the app must work on 1.3 & getStackTrace()
wasn't introduced until 1.4.

Can I still somehow get hold of the StackTrace & retrieve the line no?

Thanks

harry



Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Richard Gordon | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.