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

util.Try methods returning Try may throw exceptions instead of returning Failure

    Details

      Description

      The following code should throw no exceptions e.g. when pasted into the REPL. The solution in each case is the same: to wrap the call to the offending function or by-name parameter in the standard try/catch block.

      import util.Try
      
      val a = "apple"
      def fail: String = throw new Exception("Fail!")
      def argh: Try[String] = throw new Exception("Argh!")
      
      // None of these should throw anything
      Try(fail).orElse(argh)                                  // In Try
      Try(a).transform(_ => argh, _ => argh)                  // In Try
      Try(fail).transform(_ => argh, _ => argh)               // In Try
      Try(fail).recoverWith{ case e if (a==fail) => Try(a) }  // In Failure
      Try(fail).recoverWith{ case _ => argh }                 // In failure
      

        Activity

        Show
        Josh Suereth added a comment - https://github.com/scala/scala/pull/1269

          People

          • Assignee:
            Josh Suereth
            Reporter:
            Rex Kerr
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development