lombok 基础注解之 @SneakyThrows

最全的 lombok 注解详情(随着版本不定时更新)

一、注解介绍

作用于方法,对异常进行捕捉并抛出

二、属性介绍

  • value:指定异常类型,默认为 Throwable.class

三、实战演练

/**
 * value:指定异常类型,默认为 Throwable.class
 */
public class 刘诗诗 {
    
    
	public static void main(String[] args) {
    
    
		shishi();
	}
	
	@SneakyThrows(Exception.class)
	private static void shishi() {
    
    
		throw new Exception("刘诗诗");
	}
}
编译后
public class 刘诗诗 {
    
    
	public static void main(String[] args) {
    
    
		shishi();
	}
	
	private static void shishi() {
    
    
	    try {
    
    
	      	throw new Exception("刘诗诗");
	    } catch (Exception $ex) {
    
    
	      	throw $ex;
	    }
	}
}

四、温馨提示

当用 @SneakyThrows 注解捕获异常后,在调用方法时就不需要再捕获了

猜你喜欢

转载自blog.csdn.net/qq_39249094/article/details/121094182