Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Partner – Frontegg – expanded (cat = Security)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Partner – Frontegg – expanded (cat = Spring Security)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this article, we're going to take a quick look at Google Guava's Throwables class.

This class contains a set of static utility methods for dealing with exception handling and:

  • propagation
  • processing the cause chain

2. Maven Dependency

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.0.1-jre</version>
</dependency>

3. Propagation

Let's assume we interact with some code that throws a generic Throwable.

In most cases, we want to convert this to a RuntimeException if it's a direct subclass of Throwable.

However, if it's an instance of Error, RuntimeException or Exception we can invoke the propagateIfPossible to propagate it as-is:

try {
    methodThatMightThrowThrowable();
} catch (Throwable t) {
    Throwables.propagateIfPossible(t, Exception.class);
    throw new RuntimeException(t);
}

4. Causal Chain

Guava also provides utility methods for inspecting the thrown exception and its chain.

Throwable getRootCause(Throwable)

The getRootCause method allows us to get the innermost exception, which is useful when we want to find the initial cause.

List<Throwable> getCausalChain(Throwable)

This getCausalChain method will return a list of all the throwables in the hierarchy. This is handy if we want to check if it contains a certain type of exception.

String getStackTraceAsString(Throwable)

The getStackTraceAsString method will return the recursive stack trace of the exception.

5. Conclusion

In this tutorial, we illustrated some examples where we can use Guava's Throwables class to simplify dealing with exceptions.

As always, the complete source code is available over on GitHub.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!