DEV Community

Cover image for 🎣 Catchy — A Cleaner Way to Handle Exceptions in Java
jvm-Monster
jvm-Monster

Posted on

🎣 Catchy — A Cleaner Way to Handle Exceptions in Java

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); } 
Enter fullscreen mode Exit fullscreen mode

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(); } 
Enter fullscreen mode Exit fullscreen mode

After:

TryWrapper.tryCatch(() -> risky()) .logIfFailure(logger) .onSuccess(val -> System.out.println(val)) .onFailure(err -> System.err.println(err.getMessage())); 
Enter fullscreen mode Exit fullscreen mode

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); 
Enter fullscreen mode Exit fullscreen mode

♻️ Recovery

Gracefully fallback:

.recover(() -> "default") .recoverWithValue("fallback") .recoverWithMessage("Uh-oh!") 
Enter fullscreen mode Exit fullscreen mode

🔄 Transform values

You can transform the success result using .map():

.map(val -> val + "!") 
Enter fullscreen mode Exit fullscreen mode

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())); 
Enter fullscreen mode Exit fullscreen mode

📦 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> 
Enter fullscreen mode Exit fullscreen mode

🔗 GitHub
🔗 JitPack


🚀 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)