Ori Marko
Ori Marko

Reputation: 58772

Exception with Lombok builder included in stacktrace

Code is throwing exception using lombok builder:

throw MyException.builder().error(ErrorCode.GeneralError).message(error).build();

Stacktrace is showing the root of exception as the builder method (@Builder)

com.MyException
    at com.MyException$MyExceptionBuilder.build(MyException.java:9)
    ...

Isn't it lombok issue that builder added to stacktrace ?

Exception Class:

@Builder
public class MyException extends Exception {
    private static final long serialVersionUID = -7842978360324381658L;
    ErrorCode error;
    RequestVO request;
    ResponseVO response;
    String message;

Upvotes: 3

Views: 1215

Answers (1)

Michael Piefel
Michael Piefel

Reputation: 19968

A very interesting question indeed, which got me puzzled for a moment. The solution is the following:

The stack trace is filled in at the point of the exception’s constructor.

If you do not call fillInStackTrace() by hand, then Java will fill it in at the point where you call new. (Well, the JVM will always fill it in, but it can be overwritten.) Now where exactly is your MyException constructor called? Yes, in the build() function of your builder. Et voilà, that’s what you will see in your stack trace.

Upvotes: 2

Related Questions