gpt4 book ai didi

scala - 如何找出哪一行在 scala repl 中抛出异常?

转载 作者:行者123 更新时间:2023-12-01 13:15:54 25 4
gpt4 key购买 nike

我有一段在 scala repl 中运行的代码。代码抛出异常。如何找出那条线呢?堆栈跟踪中有行号,但它们是错误的。在下面的示例中,堆栈跟踪表明异常是在第 13 行抛出的,但代码只有 5 行。

$ scala
Welcome to Scala 2.12.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_171).
Type in expressions for evaluation. Or try :help.

scala> :paste
// Entering paste mode (ctrl-D to finish)

if (math.random > 0.5) {
throw new Exception()
} else {
throw new Exception()
}

// Exiting paste mode, now interpreting.

java.lang.Exception
... 28 elided

scala> lastException.printStackTrace
java.lang.Exception
at $line3.$read$$iw$$iw$.<init>(<console>:13)
at $line3.$read$$iw$$iw$.<clinit>(<console>)
at $line3.$eval$.$print$lzycompute(<console>:7)
at $line3.$eval$.$print(<console>:6)
at $line3.$eval.$print(<console>)
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 scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:742)

最佳答案

我有更好的运气 Ammonite REPL

@ {
if (math.random > 0.5) {
throw new Exception()
} else {
println("here")
throw new Exception()
}
}
java.lang.Exception
ammonite.$sess.cmd2$.<init>(cmd2.sc:2)
ammonite.$sess.cmd2$.<clinit>(cmd2.sc)


@

@ {
if (math.random > 0.5) {
throw new Exception()
} else {
println("here")
throw new Exception()
}
}
here
java.lang.Exception
ammonite.$sess.cmd3$.<init>(cmd3.sc:5)
ammonite.$sess.cmd3$.<clinit>(cmd3.sc)

if 语句算作第 1 行,然后第 2 行和第 5 行在这里似乎是正确的,因为它们是可以抛出异常的地方。

关于scala - 如何找出哪一行在 scala repl 中抛出异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55071595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com