By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,032 Members | 811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,032 IT Pros & Developers. It's quick & easy.

How to get line no Exception occurred at?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.