# How to throw generic exception

**URL:** <https://discuss.kotlinlang.org/t/how-to-throw-generic-exception/3672>\
**Category:** Uncategorized\
**Created:** [July 9, 2017, 6:28pm UTC](https://discuss.kotlinlang.org/t/how-to-throw-generic-exception/3672 "2017-07-09T18:28:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jschneider](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jschneider/32/3050_2.png) [@jschneider](https://discuss.kotlinlang.org/u/jschneider)\
**Post date:** [July 9, 2017, 6:28pm UTC](https://discuss.kotlinlang.org/t/how-to-throw-generic-exception/3672/1 "2017-07-09T18:28:51Z")

</div>

Hi guys,

could anybody show me how to convert that Java code to Kotlin?

`public <E extends Throwable> void foo() throws E`

And before you have to ask: “throws Throwable” is no option because that method is called from Java legacy code. And that Java code only catches “E”…

---

<div class="post-metadata">

**Author:** ![elizarov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/elizarov/32/2164_2.png) [@elizarov](https://discuss.kotlinlang.org/u/elizarov)\
**Post date:** [July 10, 2017, 8:43am UTC](https://discuss.kotlinlang.org/t/how-to-throw-generic-exception/3672/2 "2017-07-10T08:43:53Z")

</div>

You cannot declare a method with such Java signature in Kotlin, so if you want to preserve ability to invoke it from Java with this particular signature, then the best thing you can do is to declare it in Java. You can still implement it in Kotlin, though, either via delegation or via override.

---

<div class="post-metadata">

**Author:** ![jschneider](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jschneider/32/3050_2.png) [@jschneider](https://discuss.kotlinlang.org/u/jschneider)\
**Post date:** [July 10, 2017, 9:12am UTC](https://discuss.kotlinlang.org/t/how-to-throw-generic-exception/3672/3 "2017-07-10T09:12:44Z")

</div>

Thanks for your fast reply. I switched back to Java for these classes.

Any idea if this feature will be added in the future?

---

<div class="post-metadata">

**Author:** ![elizarov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/elizarov/32/2164_2.png) [@elizarov](https://discuss.kotlinlang.org/u/elizarov)\
**Post date:** [July 10, 2017, 9:58am UTC](https://discuss.kotlinlang.org/t/how-to-throw-generic-exception/3672/4 "2017-07-10T09:58:29Z")

</div>

Kotlin does not have checked exceptions and is unlikely to introduce Java-style checked exceptions (where a specific classes of checked exceptions that are thrown by the method are declared) which are needed to port this specific signature to Kotlin. One might ask for an extension of Kotlin’s `Throws` annotation to support your use-case, though, and there is a similar issue in YouTrack that you can watch: [https://youtrack.jetbrains.com/issue/KT-11774](https://youtrack.jetbrains.com/issue/KT-11774)
