A library to handle success and failure without exceptions

Wave goodbye to slow exceptions and embrace clean, efficient error handling by encapsulating operations that may succeed or fail in a type-safe way.

Results in a Nutshell

In Java, methods that can fail typically do so by throwing exceptions. Then, exception-throwing methods are called from inside a try block to handle errors in a separate catch block.

Using Exceptions

This approach is lengthy, and that's not the only problem — it's also very slow.

Conventional wisdom says exceptional logic shouldn't be used for normal program flow. Results make us deal with expected error situations explicitly to enforce good practices and make our programs run faster.

Let's now look at how the above code could be refactored if connect() returned a Result object instead of throwing an exception.

Using Results

In the example above, we used only 4 lines of code to replace the 10 that worked for the first one. But we can effortlessly make it shorter by chaining methods. In fact, since we were returning -1 just to signal that the underlying operation failed, we are better off returning a Result object upstream. This will allow us to compose operations on top of getServerUptime() just like we did with connect().

Embracing Results

Ready to Tap into the Power of Results?

Read the guide and transform your error handling today.

🌱Getting Started🪴Basic Usage🚀Advanced Usage

Also available as an ebook in multiple formats. Download your free copy now!

TL;DR

Not a fan of reading long docs? No worries! Tune in to Deep Dive, a podcast generated by NetbookLM. In just a few minutes, you'll get the essential details and a fun intro to what this library can do for you!

Last updated

Was this helpful?