Catch block entered even when there is no exception

Here is my code:

// extract data from input
…some code to calculate x,y
Right(x/ y)
case e: Exception => Left(e.getMessage)

extractData can throw exception.
What I observe is that even when no exception is thrown, still execution enters catch block.

Why this behaviour?

If catch block was entered, then an exception was thrown. I would try printing the exception message to see why there was an exception:

} catch {
  case e: Exception =>
    val message = e.getMessage

How do you know the catch block was entered? It seems to me that the only way you could know is by getting a Left, which could only be constructed from an actual Exception.

I’ll just reiterate what others have already said - you should examine the contents of the Exception in your catch block; I am partial to doing so with a debugger in an IDE, but printing the message and / or stacktrace from the Exception also work well.

Unless there is more code you haven’t shared, it is unlikely that you would receive a Left with no exception being thrown.

Yes, Left is not generated. My observation was during debugging. It may be IntelliJ debugger issue.

Might not even be an issue, just normal behavior. Keep in mind, you can’t step into everything. It is entirely normal, in any debugger, to sometimes hit an Exception and be tossed to the handler without it being obvious which line of code threw the Exception, because it came out of some library call underneath. Just because you didn’t see an explicit throw doesn’t mean there wasn’t one…

Perhaps a java.lang.Error (which is Throwable, but not Exception) was thrown?