4

In Java it's easy to know when you have to try - catch a piece of code because the compiler force you to do it and the method declaration have the throws keyword with the Exceptions that will be throw.

For example:

void testMethod() throws Exception {
    throw new Exception("test");
}

But in Kotlin would be something like:

fun testMethod() {
    throw Exception("test")
}

How can I know in Kotlin that a piece of code provided by a class need to be handled for a certain Exception?

I can't know if I have to handle a Exception, or if I can be more specific with a IOExcepcion

Edit 16/09/2021:

After more investigation, I found that there is no solution to this "problem".

There is a related question from 2016 facing the same issue: Is there any easy way to see what exceptions a Kotlin function throws?

And this answer resume very well what could be done:

If you want to know what exceptions a Java method throws when called by Kotlin from IntelliJ, you can use the F1 key shortcut to pull up the javadoc and see the throws declaration in the popup menu.

Kotlin functions can declare exceptions that it throws using the @Throws annotation. Annotations are obviously optional, so you probably can't expect this to always exist. Unfortunately, when you use the F1 keyboard shortcut on a method using @Throws, it doesn't show the exceptions declared to be thrown. Java calls into these methods are required to catch these exceptions declared in the annotation.

Kotlin javadoc can use the @throws javadoc annotation to further provide definition exceptions that can be thrown in a function. These do appear in javadoc and in F1 help popups. An of course this is also optional.

Bogdan Android
  • 1,345
  • 2
  • 10
  • 21
  • 1
    https://kotlinlang.org/docs/exceptions.html#checked-exceptions – IR42 Sep 15 '21 at 08:50
  • @IR42 Thanks for the link to documentation but after reading it, I didn't get any new information, they just say that kotlin have not checked-exceptions because "that’s not good". After all I can't know which piece of code I have to check with a try catch, I have to go deep inside to each class and method to see if code just "throw" an Exception? I don't find this easy... Any help will be appreciated – Bogdan Android Sep 15 '21 at 09:29

2 Answers2

5

The answer is that you don't know what exceptions a Kotlin method can throw other than what it says in its documentation. The designers of Kotlin deliberately left that feature out.

Specifically, they came to the conclusion that enforcing catching of exceptions was more trouble than it was worth. A lot of Java code doesn't actually really handle the checked exceptions it's supposed to, and ends up spending a lot of boilerplate just throwing them away or logging them and ignoring them. Checked exceptions are particularly difficult to handle with functional programming, where you have to have specialized lambdas that can throw checked exceptions.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
0

Kotlin doesn't have checked exceptions, consequentially there is no catch or specify requirement. So there is no need to specify any checked exception in the function header.

This also means that kotlin will not force you to catch checked exceptions thrown from any java code. its upto you if you want to catch such exception or you can choose not to.

If you want to interoperate with java then you can use @Throws

@Throws(IOException::class)
fun readFile(name: String): String {...}

// this will be translated to

String readFile(String name) throws IOException {...}
mightyWOZ
  • 7,946
  • 3
  • 29
  • 46
  • 2
    I understand, but imagine that I call readFile() from my code, the compiler don't say nothing so I don't surround it with a try catch, readFile() find an error and throw me an IOException, my code just explode and the app close. So... how do I know that I have to use the try catch with this readFile() method if any signature show me it even can throw an exception? I understand @Throws(IOException::class) but is mean to be used for interoperate with java, but imagine this code is just for Kotlin, no signature, how i know it can throw an IOExeption? – Bogdan Android Sep 15 '21 at 09:34
  • You can always read the method signature of `readFile` to know what exceptions it can throw and catch them as you want. kotlin compiler will not complain even if you don't catch checked exceptions – mightyWOZ Sep 15 '21 at 09:48
  • 1
    But imagine you are using another method inside readFile that throws the Exception, so you have to check the readFile method and the others methods that are called inside readFile, thats look like a cascade of methods to just know when you have to check for exceptions. After all, in Kotlin you have not method signature with "throws" keyword so it's not easy to identificate when to try catch... I find all this very confuse.... and having to check method to method... I don't get it, looks to dificult and time wasting ... – Bogdan Android Sep 15 '21 at 09:59
  • 1
    If a Java method calls methods that throw checked exceptions, it has to declare it in the signature, so you don't have to dig any deeper than looking at the signature of the method you're calling. And if you're working with a Kotlin library, it shouldn't be exposing public functions that throw checked exceptions without using `@Throws` if it's designed correctly. In your project code, you can use `runCatching` and the `Result` class to swallow any checked exceptions at the lowest level possible. – Tenfour04 Sep 15 '21 at 14:03