Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 707
user7294900 Avatar asked Dec 04 '25 18:12

user7294900


1 Answers

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.

like image 71
Michael Piefel Avatar answered Dec 06 '25 06:12

Michael Piefel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!