user773366
user773366

Reputation:

Exception handling with Akka actors

Does the exception generated by a function within the child actor have to be caught and thrown by the child actor explicitly or will the supervisor strategy directives(escalate) take care of percolating the generated exception to the supervisor actor behind the scenes?

My supervisor strategy:

  override val supervisorStrategy =
OneForOneStrategy(maxNrOfRetries = 5, withinTimeRange = 5 minute) {
  case _: ArithmeticException      ⇒ Resume
  case _: NullPointerException     ⇒ Restart
  case _: IllegalArgumentException ⇒ Stop
  case _: IOException              ⇒ Stop
  case _: Exception                ⇒ Restart
}   

And some of the operations within the child actor could possibly throw an IOException. Should I put a try catch block in the child actor to catch it and then throw it so that the supervisor can catch it? Or will akka take care of it behind the scenes?

Upvotes: 3

Views: 1579

Answers (2)

Carlos Vilchez
Carlos Vilchez

Reputation: 2804

In case you have nothing to lose, let it crash. It should be the default behaviour. But unfortunately sometimes you need to allocate resources or you need to information about what happened when things went wrong.

Personally I usually catch all the exceptions I would like to inform about what is going on when I am creating endpoints or web applications.

Upvotes: 1

Quizzie
Quizzie

Reputation: 879

Most of the time it's best to "let it crash" and choose the correct supervisor strategy for your situation. If you want to keep the internal state, just choose resume. Cleaning up resources before restarting can be done in the preRestart method.

Akka has sophisticated ways of handling exceptions, why not use them? There is no reason to catch an exception and then throw it again.

Upvotes: 0

Related Questions