Guava's Throwables
utility can frequently simplify dealing with exceptions.
Sometimes, when you catch an exception, you want to throw it back up to the next try/catch block. This is frequently the case for RuntimeException
or Error
instances, which do not require try/catch blocks, but can be caught by try/catch blocks when you don't mean them to.
Guava provides several utilities to simplify propagating exceptions. For example:
for (Foo foo : foos) { try { foo.bar(); } catch (BarException | RuntimeException | Error t) { failure = t; } } if (failure != null) { throwIfInstanceOf(failure, BarException.class); throwIfUnchecked(failure); throw new AssertionError(failure); }
Here are quick summaries of the propagation methods provided by Guava:
NOTE: We deprecated Throwables.propagate(Throwable)
in v20.0. Read about why.
Guava makes it somewhat simpler to study the causal chain of an exception, providing three useful methods whose signatures are self-explanatory:
Throwable getRootCause(Throwable)
List<Throwable> getCausalChain(Throwable)
String getStackTraceAsString(Throwable)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.3