Lombok annotation-@SneakyThrows

@SneakyThrowsThe use of annotations starts with the exception design system of Java.
There are two common types of exceptions in java.
1. Ordinary Exceptionclass, which is what we often call checked exception or Checked Exception.
2. RuntimeExceptionClass, both runtime exception.

The former will force its method declaration throws to be thrown, and the caller must explicitly handle the exception. The purpose of the design is to remind developers to deal with some abnormal situations that may inevitably exist in some scenarios. For example, network abnormalities cause IOException.

But in reality, things often backfire. In most cases of anomalies, we throw things out all the way. (I can't handle the forced processing! The concubine can't do it) So gradually the common method for java programmers to deal with Exception is to wrap RuntimeException on the outside, and then throw it up. This kind of solution idea appears everywhere especially in Spring. See "Spring in Action"

try {
    
    

} catch (Exception e){
    
    
   throw new RuntimeException(e);
}

Lombok's @SneakyThrows is to eliminate such template code.
No need to worry about Exception handling after using annotations


 import lombok.SneakyThrows;

public class SneakyThrowsExample implements Runnable {
    
    
  @SneakyThrows(UnsupportedEncodingException.class)
  public String utf8ToString(byte[] bytes) {
    
    
    return new String(bytes, "UTF-8");
  }
  
  @SneakyThrows
  public void run() {
    
    
    throw new Throwable();
  }
}

Really generated code

import lombok.Lombok;

public class SneakyThrowsExample implements Runnable {
    
    
  public String utf8ToString(byte[] bytes) {
    
    
    try {
    
    
      return new String(bytes, "UTF-8");
    } catch (UnsupportedEncodingException e) {
    
    
      throw Lombok.sneakyThrow(e);
    }
  }
  
  public void run() {
    
    
    try {
    
    
      throw new Throwable();
    } catch (Throwable t) {
    
    
      throw Lombok.sneakyThrow(t);
    }
  }
}

principle

Obviously the magic is hidden in Lombok.sneakyThrow(t);it. Everyone might think that this method is something like new RuntimeException(). However, it is not. Reading the code, we can see that the core logic of the whole method is throw (T)t;to use generics to force the Throwable we passed in to RuntimeException. Although in fact we are not RuntimeException. But it does not matter. Because the JVM does not care about this. When generics are finally stored as bytecode, there is no generic information. This is just to fool the javac compiler. There is an explanation in the comments in the source code.

    public static RuntimeException sneakyThrow(Throwable t) {
    
    
        if (t == null) throw new NullPointerException("t");
        return Lombok.<RuntimeException>sneakyThrow0(t);
    }

    private static <T extends Throwable> T sneakyThrow0(Throwable t) throws T {
    
    
        throw (T)t;
    }

Original author: lazyguy
description link: https://www.jianshu.com/p/7d0ed3aef34b

Guess you like

Origin blog.csdn.net/qq_35448165/article/details/109248571