注解类型 SneakyThrows


@Target({METHOD,CONSTRUCTOR}) @Retention(SOURCE) public @interface SneakyThrows
@SneakyThrows 将避免 javac 坚持您捕获或继续抛出方法体中声明它们生成的任何已检查异常。

@SneakyThrows 不会静默地吞噬、包装到 RuntimeException 中,或以其他方式修改任何列出的已检查异常类型的异常。JVM 不检查已检查异常系统的一致性;javac 会检查,并且此注解允许您选择退出其机制。

完整文档请见 @SneakyThrows 的 project lombok 功能页面

示例

 @SneakyThrows(UnsupportedEncodingException.class)
 public String utf8ToString(byte[] bytes) {
     return new String(bytes, "UTF-8");
 }
 
变为
 public String utf8ToString(byte[] bytes) {
     try {
         return new String(bytes, "UTF-8");
     } catch (UnsupportedEncodingException $uniqueName) {
         throw useMagicTrickeryToHideThisFromTheCompiler($uniqueName);
         // This trickery involves a bytecode transformer run automatically during the final stages of compilation;
         // there is no runtime dependency on lombok.
     }
 
  • 可选元素摘要

    可选元素
    修饰符和类型
    可选元素
    描述
    Class<? extends Throwable>[]
     
  • 元素详情

    • Class<? extends Throwable>[] value
      返回
      您想要偷偷抛出的异常类型。
      默认
      {java.lang.Throwable.class}