Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] @SneakyReThrow #3771

Open
cristcost opened this issue Oct 30, 2024 · 0 comments
Open

[FEATURE] @SneakyReThrow #3771

cristcost opened this issue Oct 30, 2024 · 0 comments

Comments

@cristcost
Copy link

Describe the feature
Background: I don't like SneakyThrow because it violates the Java contracts, but I'd love a boilerplate saving functionality to reduce exception handling code.

This feature request is to ask for a @SneakyReThrow annotation, which doesn't throw the checked annotation directly, but instead rethrows a different exception (by default, a RuntimeException) with the original annotation as cause and additionally supports a message.

Example of how the documentation would look like:

WITH LOMBOK

public class SneakyReThrowsExample implements Runnable {

  // similar to @SneakyThrows(UnsupportedEncodingException.class)
  @SneakyReThrows(
      catching = UnsupportedEncodingException.class,
      rethrowing = RuntimeException.class,
      message = "Error while parsing bytes to string")
  public String utf8ToString(byte[] bytes) {
    return new String(bytes, "UTF-8");
  }

  // similar to @SneakyThrows({IOException.class, ClassNotFoundException.class})
  @SneakyReThrows(
      catching = {IOException.class, ClassNotFoundException.class},
      rethrowing = RuntimeException.class,
      message = "Error while loading class from configuration")
  public Class<?> getMyClass() {
    String className = Files.readString(Path.of("classname.config"));
    return Class.forName(className);
  }

  // similar to @SneakyThrows
  @SneakyReThrows
  public void run() {
    throw new Throwable();
  }
}

VANILLA JAVA

public class SneakyReThrowsExample implements Runnable {

  public String utf8ToString(byte[] bytes) {
    try {
      return new String(bytes, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      throw new RuntimeException("Error while parsing bytes to string", e);
    }
  }
  public Class<?> getMyClass() {
    try {
      String className = Files.readString(Path.of("classname.config"));
      return Class.forName(className);
    } catch (IOException | ClassNotFoundException e) {
      throw new RuntimeException("Error while loading class from configuration", e);
    }
  }

  public void run() {
    try {
      throw new Throwable();
    } catch (Throwable e) {
      throw new RuntimeException(e);
    }
  }
}

Describe the target audience
Everyone willing to use a boilerplate reduction annotation for handling exceptions but not using @SneakyThrow because it violates java contracts

Additional context
If anyone can suggest where to add this functionality in the code (I imagine it's similar to @SneakyThrow) please share info so to attempt a pull request

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant