# Introduction

<picture><source srcset="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-0f21170996b221cdf3b1e2e6af64d4c496f940fe%2Fresult-logo.dark.svg?alt=media" media="(prefers-color-scheme: dark)"><img src="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-0a47d42244b53b23052663b79bf353283bf0678e%2Fresult-logo.svg?alt=media" alt=""></picture>

## Handle success and failure in Java 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.

<table data-view="cards"><thead><tr><th align="center"></th><th align="center"></th><th align="center"></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td align="center"><picture><source srcset="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-7a68face31f3e9a1f3343b10e05f822bfe3c9456%2Ftachometer-alt.dark.svg?alt=media" media="(prefers-color-scheme: dark)"><img src="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-725aa7cc28266e34222793f1f6632343a01676a4%2Ftachometer-alt.svg?alt=media" alt="Because exceptions are so last century."></picture></td><td align="center"><strong>Boost Performance</strong></td><td align="center">Avoid exception overhead and benefit from faster operations</td><td><a href="extra/benchmarks">benchmarks</a></td></tr><tr><td align="center"><picture><source srcset="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-79f442d6d9339fab7d22516e47efa00c1f9a6ed6%2Ftint.dark.svg?alt=media" media="(prefers-color-scheme: dark)"><img src="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-572e2790e2a6e3bd0dc6e5b766a3ab2831f4ceea%2Ftint.svg?alt=media" alt="For a smooth ride from Optional to Result."></picture></td><td align="center"><strong>Simple API</strong></td><td align="center">Leverage a familiar interface for a smooth learning curve</td><td><a href="docs/start">start</a></td></tr><tr><td align="center"><picture><source srcset="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-7b388705214a0139596e5d32d1443e8274e1af22%2Fbolt.dark.svg?alt=media" media="(prefers-color-scheme: dark)"><img src="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-c1cce6b90fc0c12e81ccc64a1945e825ddf75623%2Fbolt.svg?alt=media" alt="Say goodbye to error-handling acrobatics."></picture></td><td align="center"><strong>Streamlined Error Handling</strong></td><td align="center">Handle failure explicitly to simplify error propagation</td><td><a href="docs/advanced/screening">screening</a></td></tr><tr><td align="center"><picture><source srcset="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-c1650ac0f59befab10f18c86de69236e66c41bdc%2Fshield-alt.dark.svg?alt=media" media="(prefers-color-scheme: dark)"><img src="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-4f6c6e9a0b07b31dbf0df6f7827456017bb4d31f%2Fshield-alt.svg?alt=media" alt="Skip the exception rollercoaster and enjoy the smooth ride."></picture></td><td align="center"><strong>Safe Execution</strong></td><td align="center">Ensure safer and more predictable operation outcomes</td><td><a href="docs/basic/checking">checking</a></td></tr><tr><td align="center"><picture><source srcset="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-e6692fc8a728bcf1f27afc4ce19a05998642e6bd%2Fglasses.dark.svg?alt=media" media="(prefers-color-scheme: dark)"><img src="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-782869bcc584590736a893adf5bc8c3c7dbd750e%2Fglasses.svg?alt=media" alt="So you can actually understand your own code next month."></picture></td><td align="center"><strong>Enhanced Readability</strong></td><td align="center">Reduce complexity to make your code easier to understand</td><td><a href="docs/basic/conditional">conditional</a></td></tr><tr><td align="center"><picture><source srcset="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-7cbb13d905e9c2b609a0969abd2063ffb67b36a2%2Ffilter.dark.svg?alt=media" media="(prefers-color-scheme: dark)"><img src="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-d4806b9bb7c6220a98e88d9b1dfed63fd964e09c%2Ffilter.svg?alt=media" alt="For those who prefer elegance over chaos."></picture></td><td align="center"><strong>Functional Style</strong></td><td align="center">Embrace elegant, functional programming paradigms</td><td><a href="docs/advanced/transforming">transforming</a></td></tr><tr><td align="center"><picture><source srcset="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-13dd485f33742f709909ac137919a2dc8f0c42c9%2Ffeather-alt.dark.svg?alt=media" media="(prefers-color-scheme: dark)"><img src="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-5cd052a29d1c7c121b4f5127164ff60016498239%2Ffeather-alt.svg?alt=media" alt="Because bloated libraries are so overrated."></picture></td><td align="center"><strong>Lightweight</strong></td><td align="center">Keep your project slim with no extra dependencies</td><td><a href="docs/start/adding-dependency">adding-dependency</a></td></tr><tr><td align="center"><picture><source srcset="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-b6ea70bff16c9f1e3d629f0857bb2c0d4ffb9815%2Fbalance-scale.dark.svg?alt=media" media="(prefers-color-scheme: dark)"><img src="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-e69b8c72c9e9f96f02e45d5e3926e69d712c0500%2Fbalance-scale.svg?alt=media" alt="Feel free to tweak and share — no strings attached."></picture></td><td align="center"><strong>Open Source</strong></td><td align="center">Enjoy transparent, permissive Apache 2 licensing</td><td><a href="extra/license">license</a></td></tr><tr><td align="center"><picture><source srcset="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-1cce85eb136a1fef925c02cadd99104069087e83%2Fmug-hot.dark.svg?alt=media" media="(prefers-color-scheme: dark)"><img src="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-5710b36fc6879babe50d87fe5c04c56b2b7acb8d%2Fmug-hot.svg?alt=media" alt="Whether you&#x27;re stuck in the past or embracing the future, we&#x27;ve got you covered."></picture></td><td align="center"><strong>Pure Java</strong></td><td align="center">Seamless compatibility from JDK8 to the latest versions</td><td><a href="https://github.com/LeakyAbstractions/result/">https://github.com/LeakyAbstractions/result/</a></td></tr></tbody></table>

{% hint style="success" %}
`Result` objects represent the outcome of an operation, removing the need to check for null. Operations that succeed produce results encapsulating a *success* value; operations that fail produce results with a *failure* value. Success and failure can be represented by whatever types make the most sense for each operation.
{% endhint %}

### 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.

<div data-full-width="true"><img src="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-d9f42c6766637a6289cfe7bf8eaefb5bd0ae22a8%2Fusing-exceptions.png?alt=media" alt="Using Exceptions"></div>

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

{% hint style="info" %}
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](https://result.leakyabstractions.com/extra/benchmarks).
{% endhint %}

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

<div data-full-width="true"><img src="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-3a484bbc01c680b2ee7654ecd8dcc2b2f6b7ac2c%2Fusing-results.png?alt=media" alt="Using Results"></div>

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()`.

<div data-full-width="true"><img src="https://137539276-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MfavUYGNGvzkCicVu3-%2Fuploads%2Fgit-blob-f7ce8795b94a9b9ddb447567ed1e756e67048ccd%2Fembracing-results.png?alt=media" alt="Embracing Results"></div>

{% hint style="success" %}
`Result` objects are immutable, providing thread safety without the need for synchronization. This makes them ideal for multi-threaded applications, ensuring predictability and eliminating side effects.
{% endhint %}

### Ready to Tap into the Power of Results?

Read the guide and transform your error handling today.

{% content-ref url="docs/start" %}
[start](https://result.leakyabstractions.com/docs/start)
{% endcontent-ref %}

{% content-ref url="docs/basic" %}
[basic](https://result.leakyabstractions.com/docs/basic)
{% endcontent-ref %}

{% content-ref url="docs/advanced" %}
[advanced](https://result.leakyabstractions.com/docs/advanced)
{% endcontent-ref %}

Also available as an **ebook** in multiple formats. [Download your free copy now!](https://leanpub.com/result/)

### TL;DR

Not a fan of reading long docs? No worries! Tune in to *Deep Dive*, a podcast generated by [NetbookLM](https://notebooklm.google.com/). In just a few minutes, you'll get the essential details and a fun intro to what this library can do for you!

{% embed url="<https://www.youtube.com/watch?v=sTBRnm0KosM>" %}
