is there a better way to write this without using ...
# arrow
h
is there a better way to write this without using the
try catch
Copy code
override fun f1(): Either<Errors.ApplicationError, AWSClient> =
            Try {
                try {
                    // logic1 here
                } catch (ex: Exception) {
                   // if logic1 throws exception then try logic2 in here
                }
            }.toEither().mapLeft {
                (Errors.UnexpectedError("Unexpected error: ${it}"))
            }
k
Something like this maybe?
Copy code
kotlin
fun f1(): IO<AWSClient> = IO {
  tryToConnect()
}.handleErrorWith { ignored: Throwable ->
  IO { tryADifferentApproach() }
}
and then use the AWSClient inside the IO via map and flatMap
k
Try
has the capability to recovery form exception
recoverWith
and it also safe agains exception if you return a
Try
again.
👍 2
Copy code
fun f1() = Try{ logic1() }
  .recoverWith{ Try{ logic2() } }
  .toEither()
h
thanks guys. recoverWith seems neater so i'll stick with that
r
If you want to make it FP you should not use
Try
unless the function is already suspended. Try can't support side effects suspension and is therefore impure. IO is the best option here as @kartoffelsup pointed out.
Copy code
Try { boom() } // runs `boom()`
IO { boom() } // suspends `boom()` until you are ready to run
🎉 1