ncredinburgh / JavaForSmallTeams

Guidance on writing "good" server side java
https://www.gitbook.com/book/ncrcoe/java-for-small-teams
Other
69 stars 31 forks source link

Guava Team recomments using Throwables.propagate() less #2

Closed arend-von-reinersdorff closed 8 years ago

arend-von-reinersdorff commented 8 years ago

Regarding the handling of checked exceptions https://ncrcoe.gitbooks.io/java-for-small-teams/content/style/900_avoid_checked_exceptions.html

The Guava Team recommends using Throwables.propagate() only for Exception and Throwable. In this case it is unknown if the throwable is checked and must be wrapped or if it is unchecked and can be rethrown directly. Eg

try {
  myObject.methodThrowingException();
} catch (Exception e) {
  throw Throwables.propagate(e);
}

Known checked or unchecked exceptions can be handled directly, eg

try {
  myObject.methodThrowingException();
} catch (KnownCheckedException e) {
  throw new RuntimeException(e);
}catch (KnownUncheckedException e) {
  throw e;
}

See Removable Throwables.propagate calls https://plus.google.com/+googleguava/posts/E2UbsJEUU

hcoles commented 8 years ago

Thanks for the feedback.

I think section needs a bit of work.

Your link to google plus doesn't work for me - but you are correct that blanket usage of Throwables.propagate isn't right.

I've updated the text in this commit

If you can suggest any improvement please do so.

arend-von-reinersdorff commented 8 years ago

That was quick! Thanks for the change, very well said :-)

Sorry, it seems I cut off two letters from the link: https://plus.google.com/+googleguava/posts/E2UbsJEUUzk