0

This is a follow up question to https://stackoverflow.com/a/55440851/2691976

I have the following code

import scala.io.Source
import scala.util.Using

object Problem {
  def main(args: Array[String]): Unit = {
    Using(Source.fromFile("thisfileexists.txt")) { source =>
      println(1 / 1)
      println(1 / 0)
    }
  }
}

Running it with scala3, it will just print out 1 single line and no error.

scala3 test.scala
1

I am expecting an error like the following,

Exception in thread "main" java.lang.ArithmeticException: / by zero
        at Problem$.main(test.scala:10)
        at Problem.main(test.scala)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at dotty.tools.scripting.ScriptingDriver.compileAndRun(ScriptingDriver.scala:42)
        at dotty.tools.scripting.Main$.main(Main.scala:43)
        at dotty.tools.MainGenericRunner$.run$1(MainGenericRunner.scala:230)
        at dotty.tools.MainGenericRunner$.main(MainGenericRunner.scala:239)
        at dotty.tools.MainGenericRunner.main(MainGenericRunner.scala)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at coursier.bootstrap.launcher.a.a(Unknown Source)
        at coursier.bootstrap.launcher.Launcher.main(Unknown Source)

So why does it not print out error when I am using Using (which I suspect it is causing the problem here)?

And what is the solution so I can use both Using and Source.fromFile with potential error?


I have read the Using Scala 2 doc and Scala 3 doc but it doesn't say anything about error


In case this is important, I am using Mac

scala3 --version
Scala code runner version 3.1.2-RC1-bin-20211213-8e1054e-NIGHTLY-git-8e1054e -- Copyright 2002-2021, LAMP/EPFL
pk028382
  • 95
  • 1
  • 10
  • `Using` catches all errors and returns a `Try` with the success of the error, you may `.get` at the end to force a rethrow. Or you may use pattern matching to properly log the error. – Luis Miguel Mejía Suárez Apr 30 '22 at 01:46

1 Answers1

0

Thats because Using returns Try as you can see here https://www.scala-lang.org/api/2.13.x/scala/util/Using$.html#apply[R,A](resource:=%3ER)(f:R=%3EA)(implicitevidence$1:scala.util.Using.Releasable[R]):scala.util.Try[A]

You can use .fold, .get, pattern matching, etc.

for example:

import scala.io.Source
import scala.util.Using

object Problem {
  def main(args: Array[String]): Unit = {
    Using(Source.fromFile("thisfileexists.txt")) { source =>
      println(1 / 1)
      println(1 / 0)
    }.get
  }
}

or as follows:

import scala.io.Source
import scala.util.Using
import scala.util._

object Problem {
  def main(args: Array[String]): Unit = {
    Using(Source.fromFile("thisfileexists.txt")) { source =>
      println(1 / 1)
      println(1 / 0)
    } match {
        case Success(res) => println("Do something on sucess")
        case Failure(ex) => println(s"Failed with ex: ${ex.getMessage}")
    }
  }
}

You can read more about Try at scala: https://www.scala-lang.org/api/2.13.x/scala/util/Try.html

Zvi Mints
  • 1,072
  • 1
  • 8
  • 20