469,270 Members | 1,764 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

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 14814
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
By using this site, you agree to our Privacy Policy and Terms of Use.