1

Possible Duplicate:
Java exception handling

In Java, is it possible to print in console the error code line number with error information means:

try {
    // something
} catch(Exception e) {
    // modify this one to print line number of error with additional info
    System.out.println(e);
}
0

5 Answers 5

9

Yep.

catch(Exception e) {
    e.printStackTrace();
}

Or you could simply throw the exception, that'll also get you a stack trace.

Sign up to request clarification or add additional context in comments.

Comments

5

Just use

e.printStackTrace();

or let the exception bubble up, if you want your program to halt on the exception.

Comments

4

You're probably looking for e.printStackTrace().

Comments

2

Throwable (which Exception extends) has a method getStackTrace() which returns a StackTraceElement[], each of which includes a method getLineNumber().

So:

e.getStackTrace()[0].getLineNumber()

Comments

2

Note that if you don't want the complete stack trace, the Exception method getStackTrace() gives you an array of StackTraceElements. You can interrogate these for line numbers, file names etc. and generate some custom informative message highlighting the source of the exception.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.