# Enumeration valueOf() throws undeclared exceptions

**URL:** https://discuss.kotlinlang.org/t/enumeration-valueof-throws-undeclared-exceptions/24592
**Category:** Multiplatform
**Created:** [April 12, 2022, 2:19pm UTC](https://discuss.kotlinlang.org/t/enumeration-valueof-throws-undeclared-exceptions/24592 "2022-04-12T14:19:05Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![merrill77](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/merrill77/32/4659_2.png) [@merrill77](https://discuss.kotlinlang.org/u/merrill77)
#### Post date: [April 12, 2022, 2:19pm UTC](https://discuss.kotlinlang.org/t/enumeration-valueof-throws-undeclared-exceptions/24592/1 "2022-04-12T14:19:05Z")

</div>

[These docs](https://kotlinlang.org/docs/enum-classes.html#working-with-enum-constants) say:

> The `valueOf()` method throws an `IllegalArgumentException` if the specified name does not match any of the enum constants defined in the class.

But my testing shows that on the Javascript platform, an IllegalStateException is thrown. On native platform (Windows), it throws kotlin.Exception.

Am I looking at the wrong documentation?

---

<div class="post-metadata">

### Author: ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)
#### Post date: [April 12, 2022, 6:39pm UTC](https://discuss.kotlinlang.org/t/enumeration-valueof-throws-undeclared-exceptions/24592/2 "2022-04-12T18:39:01Z")

</div>

~~Just do:~~

```kotlin
fun <T : Enum<T>> T.valueOfOrNull(name: String) = 
    kotlin.runCatching { valueOf(name) }.getOrNull()

```

~~Then handle the `null` instead of a pesky exception.~~

Watch post below!

---

<div class="post-metadata">

### Author: ![madmax1028](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/madmax1028/32/3335_2.png) [@madmax1028](https://discuss.kotlinlang.org/u/madmax1028)
#### Post date: [April 12, 2022, 7:36pm UTC](https://discuss.kotlinlang.org/t/enumeration-valueof-throws-undeclared-exceptions/24592/3 "2022-04-12T19:36:18Z")

</div>

It won’t work, because you need to call a static method, not a member function on some enum instance. To make it work you would write this:

```kotlin
inline fun <reified T : Enum<T>> enumValueOfOrNull(name: String): T? =
	runCatching { enumValueOf<T>(name) }.getOrNull()

```

---

<div class="post-metadata">

### Author: ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)
#### Post date: [April 14, 2022, 5:26am UTC](https://discuss.kotlinlang.org/t/enumeration-valueof-throws-undeclared-exceptions/24592/4 "2022-04-14T05:26:49Z")

</div>

You can follow the issue [KT-35116](https://youtrack.jetbrains.com/issue/KT-35116) which tracks this bug.
