You don’t need a try-catch anymore!

Haluan Mohammad Irsad
4 min readFeb 17, 2025

--

Ilustration: Supervising with calmness and ease

I’ve been using Java since 2011 until now. The things that I love about this language are its mature ecosystem and enterprise-grade libraries available for free. However, the thing that I hate is handling exceptions with try-catch.

Let’s say we have code like this:

As we can see, when we call the credit function from the Account model, we need to handle try-catch in CreditService.

Let’s write a unit test for CreditService

The code runs smoothly because we have try-catch in CreditService. Let’s remove try-catch from CreditService, then re-run the test. The JUnit test fails and logs an error stack trace.

AKKA Actor

AKKA Actor is a library that can empower our Java code with an actor model. I don’t want to dive deep into AKKA and the actor model in this article, but AKKA can help clean our code from try-catch hell. Yes, “hell” and “try-catch” are two words that shouldn’t work together to attack us, Java developers. Ideally, try-catch should help us stay far from hell. However, when there are a lot of exceptions to handle, we must put try-catch in multiple places. The code below illustrates try-catchhell. I think thetry-catch hell below is still the easiest hell to deal with

try {
account.credit(amount);
System.out.println("Credited " + amount + " to account " + account.getAccountNumber() + ". New balance: " + account.getBalance());
} catch (IllegalArgumentException e) {
System.out.println("Failed to credit account: " + e.getMessage());
} catch (NullPointerException e) {
System.out.println("Null account: " + e.getMessage());
} catch (OutOfMemoryError e) {
System.out.println("Out of memory: " + e.getMessage());
} catch (Exception e) {
System.out.println("Unknown exception: " + e.getMessage());
}

How can AKKA help us out of that hell?

AKKA is an actor-based model. The Actor has a supervisor, and when an exception occurs, we just let it crash and let the Actor Supervisor handle it.

As we can see from the code above, we changed CreditService to be an AKKA Actor. Usually, the class name would be CreditActor, but I intentionally kept it as CreditService to illustrate that the same Java class we had before can now act as an actor and handle exceptions using AKKA Actor supervision strategy.

Considering that IllegalArgumentException occurs due to a negative amount, we can assume that this exception won’t break the flow. We can then tell AKKA to resume, meaning the actor continues where it left off, ignoring the error, and processing the next task (read task as message if you’re familiar with the actor model concept).

public static Behavior<CreditCommand> create() {
return Behaviors.supervise(
Behaviors.setup(CreditService::new)
).onFailure(IllegalArgumentException.class, SupervisorStrategy.resume());
}

Let’s modify our unit test to comply with AKKA actor

All test cases pass. Although we see an error stack trace, as I explained above, the Actor supervisor just ignores the error.

But, our error is too simple, the error is not just IllegalArgumentException , but can be NullPointerException and OutOfMemoryException .

However, our error is too simple. Errors are not just IllegalArgumentException but can also be NullPointerException and anything else.

As we can see above, we can take action accordingly based on different error exceptions. Such as IF we want to restart the actor while there is NullPointerException, or resume for another RuntimeException (unless NullPointerException) we can set the actor to resume, and for other Exception, we set Actor to stop.

As we can see above, we can take action accordingly based on different exceptions. For example, if we want to restart the Actor when there is a NullPointerException or resume for another RuntimeException(except NullPointerException), we can configure the Actor to resume. For other exceptions (Exception), we set the actor to stop.

One more thing, AKKA also provides SupervisorStrategy.restartWithBackoff, which allows us to define a restart with a time-based backoff, giving a moment before the Actor continues handling new tasks (read “task” as “message” if you’re familiar with the actor model concept).

Conclusion

AKKA with the Actor supervision strategy helps us write more readable code with centralized error handling. We can avoid try-catch hell.

This article is the beginning of my series on AKKA and JVM-based languages, including Java and Scala. Stay tuned for my next article, where I’ll explain the unexplained from this article.

--

--

No responses yet