Lombok - v0.10.8

lombok
Annotation Type SneakyThrows


@Target(value={METHOD,CONSTRUCTOR})
@Retention(value=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.

You should use this annotation ONLY in the following two cases:

  1. You are certain the listed exception can't actually ever happen, or only in vanishingly rare situations. You don't try to catch OutOfMemoryError on every statement either. Examples:
    IOException in ByteArrayOutputStream
    UnsupportedEncodingException in new String(byteArray, "UTF-8").
  2. You know for certain the caller can handle the exception (for example, because the caller is an app manager that will handle all throwables that fall out of your method the same way), but due to interface restrictions you can't just add these exceptions to your 'throws' clause.

    Note that, as SneakyThrow is an implementation detail and NOT part of your method signature, it is a compile time error if none of the statements in your method body can throw a listed exception.

    WARNING: You must have lombok.jar available at the runtime of your app if you use SneakyThrows, because your code is rewritten to use Lombok.sneakyThrow(Throwable).

    Example:

     @SneakyThrows(UnsupportedEncodingException.class)
     public void utf8ToString(byte[] bytes) {
         return new String(bytes, "UTF-8");
     }
     
    @SneakyThrows without a parameter defaults to allowing every checked exception. (The default is Throwable.class).

    See Also:
    Lombok.sneakyThrow(Throwable)

    Optional Element Summary
     Class<? extends Throwable>[] value
              The exception type(s) you want to sneakily throw onward.
     

    value

    public abstract Class<? extends Throwable>[] value
    The exception type(s) you want to sneakily throw onward.

    Default:
    java.lang.Throwable.class

    Lombok - v0.10.8

    Copyright © 2009-2011 The Project Lombok Authors, licensed under the MIT licence.