SneakyThrows实际上会阻止异常传播吗?

huangapple go评论107阅读模式
英文:

Does SneakyThrows actually stop an exception from propagating?

问题

我有这段代码:

  @SneakyThrows
  public void startDatabase() throws NotBoundException, RemoteException {
    entityRepository = new EntityRepositoryImpl(DatabaseManagerImpl.getInstance());
    registrar = new RegistrarImpl(entityRepository);
    String process = ManagementFactory.getRuntimeMXBean().getName();
    registrar.register();
    LOG.debug("Started database with process id: " + process);
  }

其中它抛出了异常 NotBoundException, RemoteException,添加 @SneakyThrows 注解后,IDE 报告说这两个异常不再由该方法抛出。

Lombok 的 SneakyThrows 实际上会阻止异常传播吗?

英文:

I have this code:

  @SneakyThrows
  public void startDatabase() throws NotBoundException, RemoteException {
    entityRepository = new EntityRepositoryImpl(DatabaseManagerImpl.getInstance());
    registrar = new RegistrarImpl(entityRepository);
    String process = ManagementFactory.getRuntimeMXBean().getName();
    registrar.register();
    LOG.debug("Started database with process id: " + process);
  }

In which it throws an exception NotBoundException, RemoteException adding the @SneakyThrows annotation makes the IDE report that both these two exceptions are not thrown by the method (anymore).

Does Lombok SneakyThrows actually stop an exception from propagating?

答案1

得分: 1

不,它不能阻止异常继续传播。

> @SneakyThrows可以用于在不在方法的throws子句中实际声明的情况下,偷偷抛出已检查异常。

参考链接:https://projectlombok.org/features/SneakyThrows

英文:

No, it does not prevent the exception from propagating.

> @SneakyThrows can be used to sneakily throw checked exceptions without actually declaring this in your method's throws clause.

Reference: https://projectlombok.org/features/SneakyThrows

huangapple
  • 本文由 发表于 2020年10月22日 00:21:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64467799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定