Scala Programming Language
  1. Scala Programming Language
  2. SI-6190

scala.util.Success.flatMap catching too many exceptions while it's documented to not do that

    Details

      Description

      While this is fine:

      scala> val res = scala.util.Success(1).flatMap(x => throw new Exception)
      res: scala.util.Try[Nothing] = Failure(java.lang.Exception)
      

      The documentation seems to suggest that this is not expected (the exception should be thrown instead):

      scala> val res = scala.util.Success(1).flatMap(x => throw new ThreadDeath)
      res: scala.util.Try[Nothing] = Failure(java.lang.ThreadDeath)
      

      Docs say that:

      • ''Note'': only non-fatal exceptions are caught by the combinators on `Try` (see [[scala.util.control.NonFatal]]).
      • Serious system errors, on the other hand, will be thrown.

      Within class Success, the bug seems to be here (comment added):

        def flatMap[U](f: T => Try[U]): Try[U] =
          try f(value)
          catch {
            case e => Failure(e) //XXX This should be case NonFatal(e)!
          }
      

        Activity

        Hide
        Paolo G. Giarrusso added a comment - - edited

        This was silently fixed in commit 3cb0e784a05db7d0b542cec9bf4c5fbf3772a6cf, at least judging from the source code. However, no test case was introduced for fatal exceptions — otherwise, I would close this issue.
        UPDATE: I tried addressing this in https://github.com/scala/scala/pull/1195.

        Show
        Paolo G. Giarrusso added a comment - - edited This was silently fixed in commit 3cb0e784a05db7d0b542cec9bf4c5fbf3772a6cf, at least judging from the source code. However, no test case was introduced for fatal exceptions — otherwise, I would close this issue. UPDATE: I tried addressing this in https://github.com/scala/scala/pull/1195 .

          People

          • Assignee:
            Heather Miller
            Reporter:
            Paolo G. Giarrusso
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development