A Java 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.
Boost Performance
Avoid exception overhead and benefit from faster operations
Simple API
Leverage a familiar interface for a smooth learning curve
Streamlined Error Handling
Handle failure explicitly to simplify error propagation
Safe Execution
Ensure safer and more predictable operation outcomes
Enhanced Readability
Reduce complexity to make your code easier to understand
Functional Style
Embrace elegant, functional programming paradigms
Lightweight
Keep your project slim with no extra dependencies
Open Source
Enjoy transparent, permissive Apache 2 licensing
Pure Java
Seamless compatibility from JDK8 to the latest versions
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.
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.
This approach is lengthy, and that's not the only problem — it's also very slow.
Let's now look at how the above code could be refactored if connect()
returned a Result
object instead of throwing an exception.
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()
.
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.
Read the guide and transform your error handling today.
🌱Getting Started🪴Basic Usage🚀Advanced UsageAlso available as an ebook in multiple formats. Download your free copy now!
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!
Loading...
How to add Result as a dependency to your build
Loading...
How to solve simple use-case scenarios
In this section, we'll cover foundational use cases, including checking the status of a result, unwrapping the value inside a result, and taking different actions based on success or failure.
These basics will help you handle errors more cleanly and efficiently without cluttering your code with try-catch blocks.
How to find out if the operation succeded or failed
As we discovered earlier, we can easily determine if a given Result
instance is successful or not.
We can use Result::hasSuccess
to obtain a boolean
value that represents whether a result is successful.
We can also use Result::hasFailure
to find out if a result contains a failure value.
We discussed how to determine the state of a Result object using hasSuccess
and hasFailure
. These methods provide a straightforward way to identify the outcome of an operation, helping you make decisions based on the outcome.
Loading...
How to handle success and failure scenarios
We'll now delve into a set of methods that allow you to take conditional actions based on the state of a result. They provide a cleaner and more expressive way to handle success and failure scenarios, eliminating the need for lengthy if/else blocks.
In this example, ifSuccess
ensures that the provided action (adding the success value to the list) is only executed if the parsing operation is successful.
Here, ifFailure
ensures that the provided action (adding the failure value to the list) is only executed if the parsing operation fails.
In this example, ifSuccessOrElse
simplifies conditional logic by providing a single method to handle both success and failure scenarios, making the code more concise and readable.
We explained how to handle success and failure scenarios using these three methods. They provide a powerful way to perform conditional actions based on the state of a Result, streamlining your error handling and making your code more readable and maintainable.
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...