Abhishek Kumar
Abhishek Kumar

Reputation: 769

How to print all exceptions thrown by some Scala program?

Are Scala Exceptions stored in some global exceptions table or a similar kind of a structure and if so is there any way to access it other than catch?I mean if I need to print all exceptions thrown by some Scala program what is the best way to do so?

Thanks

Upvotes: 0

Views: 300

Answers (1)

Archeg
Archeg

Reputation: 8462

There is no table where the exceptions are stored as far as I know. You can add an unhandled exception catcher for a given thread, the same way you do that in Java:

  import java.lang.Thread.UncaughtExceptionHandler
  import java.security.InvalidKeyException

  Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler {
    override def uncaughtException(t: Thread, e: Throwable): Unit = {
      println(e)
      println(e.getStackTrace.mkString("\r\n"))
    }
  })

But your exception should be unhandled in the thread for that to work. And if one unhandled exception happened - no other exceptions can be thrown - so I am not sure what do you mean by all exceptions

Upvotes: 1

Related Questions