This would be something like ``` try { foo() } c...
# arrow
l
This would be something like
Copy code
try {
  foo()
} catch(FooException) { 
} catch(BarException) { }
s
Something like this?
Copy code
fun main() {
    val tryValue: Try<Int> = Try { ... ... }
    
    val t1: Try<Number> = tryValue
        .onSpecificFailure(NumberFormatException::class) { -1 }
        .onSpecificFailure(IOException::class) { 1.3 } 
}

fun <T, T1 : T, T2 : T> Try<T1>.onSpecificFailure(tClass: KClass<out Throwable>, mapper: (Throwable) -> T2): Try<T> {
    return when (this) {
        is Try.Failure -> if (exception::class.isSubclassOf(tClass)) Try { mapper(exception) } else this
        is Try.Success -> this
    }
}
But to @Bob Glamm’s point, you may be better of using the MonadThrow/Defer constructs (and Try will be deprecated in future versions of Arrow)
b
^^ Something along this, maybe:
Copy code
fun bar(): Int = TODO()

fun <F> MonadDefer<F>.foo(): Kind<F, Int> =
    catch {
        bar()
    }.handleError { when(it) {
        is IllegalArgumentException -> 4
        is RuntimeException -> 5
        else -> 6
    }}