All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.arrow.core.nonFatalOrThrow.kt Maven / Gradle / Ivy

There is a newer version: 2.0.1
Show newest version
package arrow.core

/**
 * Returns the Throwable if NonFatal and throws it otherwise.
 *
 * @throws Throwable the Throwable `this` if Fatal
 * @return the Throwable `this` if NonFatal
 *
 * ```kotlin
 * import arrow.*
 * import arrow.core.*
 *
 * fun unsafeFunction(i: Int): String =
 *    when (i) {
 *         1 -> throw IllegalArgumentException("Non-Fatal")
 *         2 -> throw OutOfMemoryError("Fatal")
 *         else -> "Hello"
 *    }
 *
 * fun main() {
 *   val nonFatal: Either =
 *   //sampleStart
 *   try {
 *      Either.Right(unsafeFunction(1))
 *   } catch (t: Throwable) {
 *       Either.Left(t.nonFatalOrThrow())
 *   }
 *   //sampleEnd
 *   println(nonFatal)
 * }
 * ```
 * 
 *
 */
// https://youtrack.jetbrains.com/issue/KT-36036
public fun Throwable.nonFatalOrThrow(): Throwable =
  if (NonFatal(this)) this else throw this




© 2015 - 2025 Weber Informatics LLC | Privacy Policy