Iaroslav Baranov
Iaroslav Baranov

Reputation: 1120

Include source column number in addition to line number in exception stacktrace

In code

int x = 0;
int y = 1 / x;

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at sample.MyClass.main(MyClass.java:16)

How I can get somebody like

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at sample.MyClass.main(MyClass.java:16,12)[/code]

Upvotes: 0

Views: 232

Answers (2)

ToYonos
ToYonos

Reputation: 16833

It's not possible. If you want to get a more precised line number, use extra carriage returns like in this example :

yourObject.
    aMethod().
         anotherMethod().
             aLastMethod();

Upvotes: 0

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

You can't. When you print the exception it contains the StackTraceElement

This is the responsible method for above result

 public StackTraceElement(String declaringClass, String methodName,
                             String fileName, int lineNumber) {
  this.declaringClass = Objects.requireNonNull(declaringClass, "Declaring class is null");
  this.methodName     = Objects.requireNonNull(methodName, "Method name is null");
  this.fileName       = fileName;
  this.lineNumber     = lineNumber;
 }

Here there is no way to set column number.

Also StackTraceElement is a final class. you can't extend it and override either.

Upvotes: 2

Related Questions