As part of my master thesis, I have studied Akka (especially Akka Typed).
I still need to clarify why Akka throws Exceptions instead of returning Either
or Option
.
Akka is all about fault tolerance, and, in my view, the approach of using Either
and Option
is more robust than that of Exceptions.
Let me make a coupe of examples:
If an actor spawns two child actors with the same name, it throws InvalidActorNameException.
package thesis;
import akka.actor.typed.*;
import akka.actor.typed.javadsl.Behaviors;
public class NonUnique {
public static Behavior<Void> create() {
return Behaviors.setup(ctx -> {
ActorRef<Void> child1 = ctx.spawn(Behaviors.empty(), "non-unique");
ActorRef<Void> child2 = ctx.spawn(Behaviors.empty(), "non-unique");
return Behaviors.empty();
});
}
public static void main(String[] args) {
final ActorSystem<Void> system = ActorSystem.create(create(), "helloakka");
}
}
If an the actor has invalid initial behavior, it throws ActorInitializationException.
package thesis;
import akka.actor.typed.*;
import akka.actor.typed.javadsl.Behaviors;
public class InvalidInitialBehavior {
public static Behavior<Void> create() {
return Behaviors.setup(ctx -> {
ActorRef<Void> child1 = ctx.spawn(Behaviors.same(), "invalid-initial-behavior");
return Behaviors.empty();
});
}
public static void main(String[] args) {
final ActorSystem<Void> system = ActorSystem.create(create(), "helloakka");
}
}
I am mainly a Rust programmer, and I am accustomed to returning a Result
with Err
variant or an Option
in operations that can fail. Scala simulates Result
with Either
.
Why Akka decided for the Exception approach?
Is it maybe because Akka also wants to target Java users, who aren’t very familiar with Either
?