註解類型 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>[]
     
  • 元素詳細資訊

    • value

      Class<? extends Throwable>[] value
      傳回
      您想要偷偷地往上層拋出的例外類型。
      預設
      {java.lang.Throwable.class}