Lombok之@SneakyThrows使用
一. 为什么要用@SneakyThrows?
Java中的异常是一个极其重要的知识点。在阅读本文之前,读者需对异常有个基本的认识。在日常开发中,@SneakyThrows用的并不多,因为只是将异常抛出throw,还是需要你在调用方法时对异常做处理。它只是一个try-catch的简单写法:
二. @SneakyThrows如何使用?
三. @SneakyThrows源码
@Target({ ElementType.METHOD, ElementType.CONSTRUCTOR}) @Retention(RetentionPolicy.SOURCE) public @interface SneakyThrows { /** @return The exception type(s) you want to sneakily throw onward. */ Class<? extends Throwable>[] value() default java.lang.Throwable.class; //The fully qualified name is used for java.lang.Throwable in the parameter only. This works around a bug in javac: // presence of an annotation processor throws off the type resolver for some reason. }
注解属性:value是Throwable子类的数组。