GigiWithoutHadid
GigiWithoutHadid

Reputation: 369

Do I need to @Throws for every method during exception bubbling up in Kotlin?

fun fun1(id: String)
{
    addThings(3,4)
}

fun addThings(num1: Int, num2: Int)
{
    addNumbers(num1, num2)
}

@Throws(Exception::class)
fun addNumbers(num1: Int, num2: Int)
{
   throws Exception("error")
}
  1. Excption from addNumbers will bubble up all the way to fun1 is that right?
  2. Is @Throws required for addThings and fun1?

Couldn't find any docs online...

Upvotes: 0

Views: 1627

Answers (1)

Trevor
Trevor

Reputation: 1451

No.

You can find good documentation on this here.

In summary, Kotlin does not have checked exceptions. That means there is no "bubble up". There is no requirement for any of your functions to declare that they throw an exception or to use try-catch. Declaring the annotation @Throws is only strictly necessary for Java interop, you may need it if you call that Kotlin function from a Java file.

You can read about why from Kotlin's project lead Roman Elizarov.

Upvotes: 3

Related Questions