# Handling expected failures?

**URL:** <https://discuss.kotlinlang.org/t/handling-expected-failures/3413>\
**Category:** Language Design\
**Created:** [June 20, 2017, 6:31am UTC](https://discuss.kotlinlang.org/t/handling-expected-failures/3413 "2017-06-20T06:31:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mojocin](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mojocin](https://discuss.kotlinlang.org/u/mojocin)\
**Post date:** [June 20, 2017, 6:31am UTC](https://discuss.kotlinlang.org/t/handling-expected-failures/3413/1 "2017-06-20T06:31:43Z")

</div>

Hi - I am working hard as a Kotlin advocate at Cinnober, where we have a long history of Java development. Overall I find it easy to show how Kotlin is simply an improvement over Java, but there is one area where I am struggling - a replacement for checked exceptions.

I completely agree that checked exceptions lead to a whole range of problems that we do not want in Kotlin, so they are gone for a good reason. I feel though that there are cases where we want to force the caller to handle an expected failure (for instance when fetching remote resources or accessing databases) that Kotlin currently does not cover well. Scala has the solution I like best for this - Try(Failure/Success), but the limited pattern matching in Kotlin limits the usefulness of that pattern.

What is the current best practice? Are there any future plans for language features that could resolve this (such as an Either construct)?

Thank you!

---

<div class="post-metadata">

**Author:** ![Jonathan.Haas](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@Jonathan.Haas](https://discuss.kotlinlang.org/u/Jonathan.Haas)\
**Post date:** [June 20, 2017, 8:38am UTC](https://discuss.kotlinlang.org/t/handling-expected-failures/3413/2 "2017-06-20T08:38:12Z")

</div>

I feel like using null values when errors happen works nicely for simple cases. For example you could so something like

```
val result : Result? = query.result
if(result == null) {
    handleError(query.error)
    return
}

doSomethingWith(result)

```

or if you want an exception something like

```
val result = query.result ?: throw Exception(query.error)

```

---

<div class="post-metadata">

**Author:** ![vbezhenar](https://avatars.discourse-cdn.com/v4/letter/v/35a633/32.png) [@vbezhenar](https://discuss.kotlinlang.org/u/vbezhenar)\
**Post date:** [June 20, 2017, 9:50am UTC](https://discuss.kotlinlang.org/t/handling-expected-failures/3413/3 "2017-06-20T09:50:41Z")

</div>

I’ve used `Result` class (similar to Rust). Probably it’s like `Try`. Why do you think that Kotlin `when` is not sufficient for this use-case? I’ve found that it was enough for my code.

Overall I prefer exceptions for error handling. I’ve used `Result` as a sequence type, because throwing exceptions in a sequence operation will break it, and I needed to keep pipeline functioning. I think, that unless you’re in a similar situation, you should use exceptions. But you should be able to use `Try`-like class, if you want.

[gist:670d86ad5f29c9ede09fc6aca00f5466 · GitHub](https://gist.github.com/anonymous/670d86ad5f29c9ede09fc6aca00f5466) here’s my class and some sequence helpers.

---

<div class="post-metadata">

**Author:** ![mojocin](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mojocin](https://discuss.kotlinlang.org/u/mojocin)\
**Post date:** [June 20, 2017, 10:47am UTC](https://discuss.kotlinlang.org/t/handling-expected-failures/3413/4 "2017-06-20T10:47:05Z")

</div>

I agree that null works extremely well for the simple cases. My question is mostly about the not so simple cases where the reason for the failure, error codes and other error information may be useful.

---

<div class="post-metadata">

**Author:** ![mojocin](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mojocin](https://discuss.kotlinlang.org/u/mojocin)\
**Post date:** [June 20, 2017, 11:33am UTC](https://discuss.kotlinlang.org/t/handling-expected-failures/3413/5 "2017-06-20T11:33:35Z")

</div>

vbezhenar: Your Result implementation was interesting - it got me on the right track. Thank you! I managed to throw something together that lets me do:

```
val checked = fetchWebResourceAsBytes("http://someplace/someData")
when (checked) {
    is Success<ByteArray> -> doSomething(checked.result)
    is Failure -> doSomethingOnFailure(checked.error)
}

```

I had assumed that type erasure would be too much of a problem, but this worked out just fine. Thank you!

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [June 21, 2017, 4:47am UTC](https://discuss.kotlinlang.org/t/handling-expected-failures/3413/6 "2017-06-21T04:47:15Z")

</div>

You can also use `Optional` class from Java 8 which is accessible from Java. It could either contain some value or be empty. It also supports null-safe result transformation and `ifPresent{}` block. In Java 9 they promise full support for two branches (present and not present) via lambdas.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [June 21, 2017, 6:53am UTC](https://discuss.kotlinlang.org/t/handling-expected-failures/3413/7 "2017-06-21T06:53:17Z")

</div>

You can handle an expected failure as result, so return a specific sealed class (Result, Error1, Error2, …).

Otherwise look at funktionale [GitHub - MarioAriasC/funKTionale: Functional constructs for Kotlin](https://github.com/MarioAriasC/funKTionale)

---

<div class="post-metadata">

**Author:** ![mojocin](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@mojocin](https://discuss.kotlinlang.org/u/mojocin)\
**Post date:** [June 21, 2017, 7:04am UTC](https://discuss.kotlinlang.org/t/handling-expected-failures/3413/8 "2017-06-21T07:04:40Z")

</div>

Thank you - a lot of nice patterns in funKTionale!
