lombok @SneakyThrows

1233356-a7c557a63d010151.png

Annotation Type SneakyThrows

@Target({METHOD,CONSTRUCTOR})
@Retention(SOURCE)
public @interface SneakyThrows

@SneakyThrow will avoid javac's insistence that you either catch or throw onward any checked exceptions that statements in your method body declare they generate.

@SneakyThrow does not silently swallow, wrap into RuntimeException, or otherwise modify any exceptions of the listed checked exception types. The JVM does not check for the consistency of the checked exception system; javac does, and this annotation lets you opt out of its mechanism.

Complete documentation is found at the project lombok features page for @SneakyThrows.

Example:

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

Becomes:

 public void 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.
     }

Kotlin 开发者社区

1233356-4cc10b922a41aa80

国内第一Kotlin 开发者社区公众号,主要分享、交流 Kotlin 编程语言、Spring Boot、Android、React.js/Node.js、函数式编程、编程思想等相关主题。

越是喧嚣的世界,越需要宁静的思考。

发布了1571 篇原创文章 · 获赞 627 · 访问量 61万+

猜你喜欢

转载自blog.csdn.net/universsky2015/article/details/103985421
今日推荐