If you're tired of writing the same try/catch block five times a day... youโre not alone.
Java gives you full control over error handling, but with great power comes repetitive boilerplate. Every time we wrap risky code, we write the same dance:
try {
var data = risky();
log.info("Success: {}", data);
} catch (Exception e) {
log.error("Something broke", e);
}
And thatโs just success/failure. What about retrying? Backoff? Fallback values?
Youโll be adding more layers than a wedding cake ๐
Thatโs why I built Catchy
Catchy is a tiny utility that makes Java error handling:
- โ Fluent and chainable
- ๐ Retryable (with backoff)
- ๐ Transformable (with
.map()) - ๐ฅ SLF4J-loggable
- โป๏ธ Recoverable with fallback values
- ๐ก Developer-friendly
The Before & After
Before:
try {
var data = risky();
System.out.println(data);
} catch (Exception e) {
e.printStackTrace();
}
After:
TryWrapper.tryCatch(() -> risky())
.logIfFailure(logger)
.onSuccess(val -> System.out.println(val))
.onFailure(err -> System.err.println(err.getMessage()));
Same control. Cleaner code.
๐ง Why Catchy?
Catchy doesnโt replace exceptions โ it wraps them with structure. You get:
โ Less boilerplate
tryCatch(() -> riskyCode()) returns a Result<T> that you can handle safely.
๐ Retry logic
With optional delay and exponential backoff:
TryWrapper.tryCatch(() -> connect(), null, 3, 200, true);
โป๏ธ Recovery
Gracefully fallback:
.recover(() -> "default")
.recoverWithValue("fallback")
.recoverWithMessage("Uh-oh!")
๐ Transform values
You can transform the success result using .map():
.map(val -> val + "!")
If it fails, it gets caught โ safely.
๐ฅ Smart logging
SLF4J logging is built-in and detects exception severity automatically:
| Exception Type | Level |
|---|---|
NullPointerException |
WARN |
RuntimeException |
ERROR |
| Checked exceptions | INFO |
โ Real-World Example
TryWrapper.tryCatch(() -> callApi())
.recover(() -> "fallback")
.map(val -> val + "!")
.logIfFailure(logger)
.onSuccess(System.out::println)
.onFailure(err -> System.err.println("Fail: " + err.getMessage()));
๐ฆ Get it via JitPack
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.justme8code</groupId>
<artifactId>catchy</artifactId>
<version>v1.0.0</version>
</dependency>
๐ Why I made Catchy
I'm a Java dev who got tired of writing the same try/catch block over and over.
Catchy was built for real-world codebases where you want control but donโt want clutter. It's inspired by functional-style Try, but designed for normal devs who just want clean, readable exception handling.
๐ Feedback welcome!
If youโve been fighting Javaโs exception boilerplate, give Catchy a spin.
PRs, stars, and suggestions all welcome โค๏ธ
Top comments (0)