简体   繁体   中英

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM