UncaughtExceptionHandler not triggered on Linux

45 views
Skip to first unread message

Craig Street

unread,
Apr 10, 2016, 5:41:40 AM4/10/16
to scala-user
Here is the code that I am using

object Main {
def main(args: Array[String]): Unit = {
Thread.setDefaultUncaughtExceptionHandler(new AppUncaughtExceptionHandler)
throw new RuntimeException("bang!")
}

class AppUncaughtExceptionHandler extends Thread.UncaughtExceptionHandler {
def uncaughtException(thread: Thread, cause: Throwable): Unit =
{
println("uncaught")
}
}


When compiling an executing on windows, the handler is triggered, however when I build this code on Linux the exception is thrown

setting handler
java.lang.RuntimeException: bang!
        at stuff.Main$.main(Main.scala:36)
        at stuff.Main.main(Main.scala)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at scala.reflect.internal.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:70)
        at scala.reflect.internal.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:31)
        at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:101)
        at scala.reflect.internal.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:70)
        at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101)
        at scala.tools.nsc.CommonRunner$class.run(ObjectRunner.scala:22)
        at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:39)
        at scala.tools.nsc.CommonRunner$class.runAndCatch(ObjectRunner.scala:29)
        at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:39)
        at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:65)
        at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:87)
        at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:98)
        at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:103)
        at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)

The build processes are different (Windows I'm compiling using Intellij), Linux is build under Gradle. Still, i have looked at the byte code on each and see nothing untoward.

Any pointers as to what might be happening here please?

Thanks


Viktor Klang

unread,
Apr 10, 2016, 2:01:53 PM4/10/16
to Craig Street, scala-user
I wouldn't be surprised if [main]'s UEH is set prior to calling main(), and therefor not delegating to the global default. Could you verify?

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Cheers,

Craig Street

unread,
Apr 10, 2016, 3:33:00 PM4/10/16
to scala-user, craig....@gmail.com
Thanks for the response. 


println(Option(Thread.getDefaultUncaughtExceptionHandler).getOrElse("None"))
println(Option(Thread.currentThread().getUncaughtExceptionHandler).getOrElse("None").toString)

Thread.setDefaultUncaughtExceptionHandler(new AppUncaughtExceptionHandler)
Thread.currentThread().setUncaughtExceptionHandler(new AppUncaughtExceptionHandler)

println(Option(Thread.getDefaultUncaughtExceptionHandler).getOrElse("None"))
println(Option(Thread.currentThread().getUncaughtExceptionHandler).getOrElse("None").toString)

throw new RuntimeException("bang!")

results in

None
java.lang.ThreadGroup[name=main,maxpri=10]
stuff.AppUncaughtExceptionHandler@59c86ddb
stuff.AppUncaughtExceptionHandler@130d19a9
java.lang.RuntimeException: bang!
        at stuff.Main$.main(Main.scala:41)
        at stuff.Main.main(Main.scala)

Almost as if the handler is being ignored.

Viktor Klang

unread,
Apr 10, 2016, 4:25:52 PM4/10/16
to Craig Street, scala-user
How are you running the program? perhaps the main() is invoked by another main() which wraps it in a try-catch?

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Cheers,

som-snytt

unread,
Apr 14, 2016, 1:23:00 AM4/14/16
to scala-user, craig....@gmail.com

This is why I don't apply for a job at google. I thought I understood the basics of Thread API.

For the record, it's really annoying to look up API under java.lang. How many hours have I spent re-reading java.lang.String?

Anyway, on Ubuntu it works if you kick off a thread. I don't know what's special about the "main" thread. I would have expected simple hierarchical handling, for current thread, thread group and default.

package uncaught


object Main {
  def main(args: Array[String]): Unit = {
    new Thread(new Runnable {
      def run(): Unit = test()
    }).start();
    Thread sleep 1000L
  }
  def test(): Unit = {
    Console println Thread.currentThread.getThreadGroup
    Console println Thread.currentThread.getUncaughtExceptionHandler
    //Thread.setDefaultUncaughtExceptionHandler(new AppUncaughtExceptionHandler)
    Thread.currentThread.setUncaughtExceptionHandler(new AppUncaughtExceptionHandler)

    throw new RuntimeException("bang!")
  }
  class AppUncaughtExceptionHandler extends Thread.UncaughtExceptionHandler {
    def uncaughtException(thread: Thread, cause: Throwable): Unit = {
      println("uncaught")
    }
  }
}

i.e.,

apm@mara:~/tmp$ scalac uncaught.scala && scala uncaught.Main
java.lang.ThreadGroup[name=main,maxpri=10]
java.lang.ThreadGroup[name=main,maxpri=10]
uncaught
Reply all
Reply to author
Forward
0 new messages