A lightweight, zero-dependency Java library for functional-style error handling.
try-util provides a Try monad to encapsulate computations that may result in an exception, allowing you to write cleaner, more composable, and more robust code. Instead of using verbose try-catch blocks, you can chain operations, transform values, and handle failures in a declarative way.
- Encapsulate Success or Failure: A
Tryis either aSuccessholding a result or aFailureholding an exception. - Functional Composition: Chain operations with
mapandflatMap. - Flexible Error Handling: Recover from failures with
recoveror provide default values withgetOrElse. - Side Effects: Use
onSuccessandonFailurefor logging or other side effects. - Easy Unwrapping: Get the value or throw an exception with
getandgetOrElseThrow.
Add following snippet in you ~/.m2/settings.xml
<server>
<id>github</id>
<username>GITHUB USERNAME</username>
<password>NEEDS TO BE FINE GRAINED TOKEN</password>
</server>Add the following dependency to your pom.xml file:
<dependency>
<groupId>io.github.abhipdgupta</groupId>
<artifactId>try-util</artifactId>
<version>1.0.0</version>
</dependency>And add the GitHub Packages repository to your pom.xml:
<repositories>
<repository>
<id>github</id>
<name>GitHub Packages</name>
<url>https://maven.pkg.github.com/abhipdgupta/try-util-java</url>
</repository>
</repositories>Use Try.of() to wrap a computation that might throw an unchecked exception.
// Success case
Try<Integer> age = Try.of(() -> 25); // Success(25)
// Failure case
Try<Integer> error = Try.of(() -> 1 / 0); // Failure(java.lang.ArithmeticException: / by zero)Use Try.ofChecked to wrap a computation that throw checked exception
public callsomething() throws Exception{
return 11;
}
Try<Integer> checkedException = Try.ofChecked(()-> callsomething())Use map to transform the value inside a Success. If the Try is a Failure, map does nothing.
Try<String> message = age.map(a -> "Age is " + a); // Success("Age is 25")
Try<String> errorMessage = error.map(a -> "Age is " + a); // Failure(java.lang.ArithmeticException: / by zero)Use flatMap to chain operations that return a Try.
Try<Double> result = Try.of(() -> "123.45")
.map(Double::parseDouble)
.flatMap(d -> Try.of(() -> d * 2)); // Success(246.9)Use getOrElse to provide a default value in case of a failure.
Integer defaultAge = error.getOrElse(30); // 30Use recover to handle an exception and return a Success.
Try<Integer> recovered = error.recover(throwable -> {
System.out.println("Recovered from: " + throwable.getMessage());
return 0;
}); // Success(0)You can also recover from specific exception types:
Try<Integer> recoveredSpecific = Try.of(() -> {
throw new java.io.IOException();
})
.recover(java.io.IOException.class, e -> 0)
.recover(Exception.class, e -> -1); // Recovers with 0Use get() to get the value. If the Try is a Failure, it throws a TryException.
Integer myAge = age.get(); // 25
try {
Integer errorAge = error.get();
} catch (TryException e) {
System.out.println(e.getCause()); // java.lang.ArithmeticException: / by zero
}Use getOrElseThrow() to re-throw the original exception.
try {
Integer value = error.getOrElseThrow();
} catch (ArithmeticException e) {
// Handle the original exception
}To build the project and run the tests, you need to have Java 21 and Maven installed.
Clone the repository and run the following command:
mvn clean installContributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License.