0

I am developing an android application with kotlin assistance but the question is based on pure kotlin fundamentals. Below is the function which is showing some unusual behavior:

fun CatchThat(funct: () -> Unit){
try {
    funct()
}catch (ex: Error){
    ex.printStackTrace()
}
}

When I use it in my code

CatchThat {
// Proprietary Code goes in here
}
  • Debugger does not work properly(sometimes)
  • The proprietary code does not execute at all(sometimes)

Why is that behavior encountered or am I getting some concepts wrong(maybe lambdas). Any help or suggestions are heartily welcomed.(I am a tyro in kotlin)

EDIT The thing that I am doing in Proprietary code. I am trying to invoke a Thread Pool that is in turn calling a web activity. This is the best and all I could explain about it. I am sorry for that.

  • For me this code sample works... which version of kotlin are you using and what is the proprietary code you are providing? – Piwo Aug 30 '17 at 12:23
  • You aren't getting any output ? you should replace Error with Throwable to catch Exceptions too – crgarridos Aug 30 '17 at 12:25
  • `Kotlin 1.1.2` and sorry I could not provide the Proprietary code as the code is **Proprietary** –  Aug 30 '17 at 12:25
  • No @crgarridos **sometimes** is important to note here.The same code is working at one point of time without error and goes blank the next time!!! –  Aug 30 '17 at 12:28
  • try to explain what are you doing in that code. The snippet is right. Launching another thread maybe ? – crgarridos Aug 30 '17 at 12:30
  • I have edited the question @crgarridos.Please have a look at it –  Aug 30 '17 at 12:35

2 Answers2

2

try/catch will only work on the current thread. In your snippet, if some exception ocourrs in another thread, the try/catch won't work

For example:

try {
    println("Hola mundo 1!")
    println(5 / 0)
} catch (ex: Throwable) {
    println("Oups! $ex")// will be printed
}

try {
    Thread {
        println("Hola mundo 2!")
        println(5 / 0)
    }.start()
} catch (ex: Throwable) {
    println("Oups! $ex")// won't be printed
}

println("Hola mundo 3!")//The exception thrown in the external thread don't kill the current thread

For the debugging issues take a look to Android Studio threaded debugging

crgarridos
  • 8,758
  • 3
  • 49
  • 61
  • Thanks a lot @crgarridos for the answer and giving your precious time for it but **why sometimes it gives the correct output, debugger works fine and sometimes the code executes too?** –  Aug 30 '17 at 13:04
  • Just guessing, it can variate for your code, as you have a thread pool, the execution is asynchronous. And then you can have exception in one or another thread "randomly" – crgarridos Aug 30 '17 at 13:07
  • Maybe! but thanks again for giving your precious time and +1 for that!!!!I will surely mention when I will get the correct fix –  Aug 30 '17 at 13:10
  • You're welcome, sorry for not be more clean but without the code is difficult to see the problem. I think you have somethings the output, but not all the code was well executed – crgarridos Aug 30 '17 at 13:12
0

I am not sure if it sorts out the problem but it's just worth a try since all your efforts went in vein(and also because there is no error in your syntax). I guess if debugger stops on wrong line(or sometimes doesn't work), it usually means that something broken in the code cache .

Try invalidating Idea cache and restarting if you are using Idea of course.

And before doing that

I would also recommend to update Kotlin with the latest version.

Amul Mittal
  • 54
  • 1
  • 9
  • Thanks Sir!! The first bold line doesn't worked but the last line i.e updating to the latest version i.e. `1.1.3` fixes the issue.Thank you so much –  Aug 31 '17 at 06:39