Skip to main content
Version: 2.x

Exit

An Exit[E, A] value describes how fibers end life. It has two possible values:

  • Exit.Success contains a success value of type A.
  • Exit.Failure contains a failure Cause of type E.

This is how the Exit data type is defined:

sealed trait Exit[+E, +A] extends ZIO[Any, E, A] { self =>
// Exit operators
}
object Exit {
final case class Success[+A](value: A) extends Exit[Nothing, A]
final case class Failure[+E](cause: Cause[E]) extends Exit[E, Nothing]
}

We can call ZIO#exit on our effect to determine the Success or Failure of our fiber:

import zio._
import zio.Console._

import java.io.IOException

val result: ZIO[Any, IOException, Unit] =
for {
successExit <- ZIO.succeed(1).exit
_ <- successExit match {
case Exit.Success(value) =>
printLine(s"exited with success value: ${value}")
case Exit.Failure(cause) =>
printLine(s"exited with failure state: $cause")
}
} yield ()

Pre-constructed Exit Valuesโ€‹

ZIO provides several pre-constructed Exit values for common cases:

  • Exit.unit โ€” A success exit with a Unit value
  • Exit.none โ€” A success exit with a None value (type: Exit[Nothing, Option[Nothing]])

These values are useful when you need to return a pre-made exit without constructing it manually:

import zio._

// Using Exit.unit for effects that only care about success or failure
val unitExit: Exit[String, Unit] = Exit.unit

// Using Exit.none for optional values
val noneExit: Exit[String, Option[Nothing]] = Exit.none

ZIO Chat

๐ŸŽ‰ Hey there, ZIO aficionado! ๐ŸŽ‰ Step into the ZIO wonderland with me as your guide. Whether you're a functional fanatic or just dipping your toes into the ZIO ecosystem, I'm here to make your journey smooth. Got ZIO questions? I've got answers! Let's navigate the ZIO universe together and unlock its infinite possibilities!