6
在无法到达的代码中抛出新的RuntimeExceptions是否不好?
我被指派去维护一个由更熟练的开发人员编写的应用程序。我遇到了这段代码: public Configuration retrieveUserMailConfiguration(Long id) throws MailException { try { return translate(mailManagementService.retrieveUserMailConfiguration(id)); } catch (Exception e) { rethrow(e); } throw new RuntimeException("cannot reach here"); } 我很好奇投掷RuntimeException("cannot reach here")是否合理。知道这段代码来自经验丰富的同事,我可能会漏掉一些明显的东西。 编辑:这是一些答案所指的扔尸体。我认为这个问题并不重要。 private void rethrow(Exception e) throws MailException { if (e instanceof InvalidDataException) { InvalidDataException ex = (InvalidDataException) e; rethrow(ex); } if (e …