Why this function does not work?


#1

So this function has some very weird behavior.

fun tryCatch(f: () -> Unit){
    try {
        f()
    }catch (e: Error){
        e.printStackTrace()
    }
}

When i use it in my code

tryCatch {
    //code
}
  1. The debugger never stops on breakpoints inside this closure
  2. sometimes the lines where the debugger stops are completely off code
  3. sometimes the code inside is never executed

Am i not understanding something crucial about Kotlin and lambdas?

Can it be so that each line in the closure is executed in its own try {} catch {}?


#2

If debugger stops on wrong line, it usually means, that something broken in the code cache. Try invalidating Idea cache and restarting if you are using Idea of course.


#3

There is nothing special about the semantics of this code; it should just work. The issues look like the code is not getting rebuilt correctly for some reason.

Can you show an example where the lambda is not executed?