Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

throws Exception in a method with Kotlin

I'm trying to convert this Java code to Kotlin:

public class HeaderInterceptor implements Interceptor {   @Override public Response intercept(Chain chain) throws IOException {     return null;   } } 

The problem is, when I implement the methods, I get something like

class JsonHeadersInterceptor : Interceptor {     override fun intercept(chain: Interceptor.Chain?): Response? {         throw UnsupportedOperationException()     } } 

The only info I've found talking about throwing exceptions in Kotlin is THIS.

Apart from removing the question mark, because it's not necessary, why it doesn't handle the IOException the same way? What is the best approach to handle this situation?

like image 440
cesards Avatar asked Apr 10 '16 10:04

cesards


People also ask

What exception throws in method?

To specify that writeList can throw two exceptions, add a throws clause to the method declaration for the writeList method. The throws clause comprises the throws keyword followed by a comma-separated list of all the exceptions thrown by that method.

How does Kotlin handle checked exceptions?

A method that might throw a checked exception needs to declare so in its method signature using the throws keyword. If you call a method that throws a checked exception, you either need to throw it again from your function or to catch it and handle it using a try... catch block.

Does Kotlin have checked exceptions?

Kotlin does away with much of Java's boilerplate, and doing away with checked exceptions fits in with that. They encourage bad practices: Trying to handle exceptions in the wrong place, i.e. at the wrong level of abstraction, where nothing useful can be done.

How does Kotlin handle multiple exceptions?

In Kotlin you can do this: try{ } catch(e: MyException1){ } catch(e: MyException2){ } catch(e: MyException3){ } [...] True but it's no worse than the other answers on this question.


2 Answers

In Kotlin, there's no checked exceptions, no exceptions have to be declared and you aren't forced to catch any exception, though, of course, you can. Even when deriving from a Java class, you don't have to declare exceptions that a method throws.

@Throws(SomeException::class) is just intended for Java interoperability, which allows one to write a function with throws in Java signature, so that in Java it will be possible (and necessary) to handle the exception.

Instead, public API exceptions should be documented in KDoc with @throws tag.

like image 61
hotkey Avatar answered Oct 08 '22 03:10

hotkey


In Java your functions are something like this

void foo() throws IOException{     throw new IOException(); } 

But in Kotlin you can add annotation like below to force other Java classes to catch it. However, as other answers have pointed out, it doesn't have any meaning among Kotlin classes.

@Throws(IOException::class) fun foo() {     throw IOException() } 

Source kotlinlang.org

like image 43
Radesh Avatar answered Oct 08 '22 04:10

Radesh