如何使用(使用source.fromfile)并处理错误

发布于 2025-01-25 14:36:23 字数 2556 浏览 3 评论 0 原文

这是 https://stackoverflow.com/a/55440851/2691976

我有以下代码

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)
    }
  }
}

运行 以下代码,它使用scala3,它将仅打印出1条线,没有错误。

scala3 test.scala
1

我期望有以下错误的错误,

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)

那么当我使用使用时,为什么它不会打印出错误(我怀疑它在这里引起问题)呢?

解决方案是什么,因此我可以使用和 source.fromfile 使用潜在错误使用


我已经阅读了使用 scala 2 doc and scala 3 doc 3 doc <,但没有说明错误,


以防这很重要,我正在使用Mac

scala3 --version
Scala code runner version 3.1.2-RC1-bin-20211213-8e1054e-NIGHTLY-git-8e1054e -- Copyright 2002-2021, LAMP/EPFL

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伪心 2025-02-01 14:36:24

那是因为使用返回尝试,您可以在此处看到
https://www.scala-lang.org/ api/2.13.x/scala/util/使用$ .html#apply [r,a](资源:=%3ER)(f:r =%3EA)(remiteVidence $ 1:scala.util.util.using.usis.usis.user.releasable [r] ):scala.util.try [a]

您可以使用 .fold .get ,模式匹配等。

例如:

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
  }
}

或如下:

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}")
    }
  }
}

您可以在Scala中阅读有关尝试的更多信息:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文