如何以编程方式检查 scala 解释器内部发生的错误
我正在使用 scala.tools.nsc.Interpreter 执行 scala 代码片段 当代码片段正确时,一切都很好,但是当它有错误时,我的代码无法找到并愉快地继续。我想获得一个异常或方法来调用以获取解释器上次评估期间发生的任何错误。
我的代码:
import scala.tools.nsc.Interpreter
import scala.tools.nsc.Settings
object RuntimeEval {
def main(args: Array[String]): Unit = {
var msg = "fail"
val eval = new RuntimeEval
msg = eval.eval(msg,"\"success\"")
println(msg)
var anInt = 0
while(true){
println("Enter an integer")
val userInput = Console.readLine
anInt = eval.eval(anInt,userInput)
println("-->"+anInt)
println
}
}
}
class ResContainer(var value: Any)
class RuntimeEval {
val settings = new Settings
settings.classpath.value = System.getProperty("java.class.path")
val interp = new Interpreter(settings)
def eval[A <: Any](obj: A, expression: String): A={
val res = new ResContainer(obj)
interp.beQuietDuring {
interp.bind("res", res.getClass.getCanonicalName, res)
interp.interpret("res.value = "+expression)
}
val info = obj match{
case x: AnyRef => "expected type: \n"+x.getClass.getCanonicalName+"\n"
case _ => "expected type is not an AnyRef\n"
}
res.value match{
case x: A => x
case x: AnyRef => error("unexpected result type, "+info+"result type:\n"+x.getClass.getCanonicalName)
case _ => error("unexpected result type, "+info+"result type is not an AnyRef")
}
}
}
问题的一个例子:
success
Enter an integer
9/12
-->0
Enter an integer
9/0
java.lang.ArithmeticException: / by zero
at .<init>(<console>:6)
at .<clinit>(<console>)
at RequestResult$.<init>(<console>:5)
at RequestResult$.<clinit>(<console>)
at RequestResult$scala_repl_result(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$17.apply(Interpreter.scala:988)
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$17.apply(Interpreter.scala:988)
at scala.util.control.Exception$Catch.apply(Exception.scala:79)
at scala...-->0
Enter an integer
9/4
-->2
Enter an integer
ArithmeticException发生在解释器内部,那么它似乎什么也没返回,所以我的代码得到了与之前操作相同的结果,0。 怎么抓住这个?
I am executing scala code snippets using scala.tools.nsc.Interpreter
When the code snippet is correct, everything is fine, but when it is buggy, my code is unable to find out and happily goes on. I would like to get an exception or method to call to get any error that happened during last evaluation by the interpreter.
my code:
import scala.tools.nsc.Interpreter
import scala.tools.nsc.Settings
object RuntimeEval {
def main(args: Array[String]): Unit = {
var msg = "fail"
val eval = new RuntimeEval
msg = eval.eval(msg,"\"success\"")
println(msg)
var anInt = 0
while(true){
println("Enter an integer")
val userInput = Console.readLine
anInt = eval.eval(anInt,userInput)
println("-->"+anInt)
println
}
}
}
class ResContainer(var value: Any)
class RuntimeEval {
val settings = new Settings
settings.classpath.value = System.getProperty("java.class.path")
val interp = new Interpreter(settings)
def eval[A <: Any](obj: A, expression: String): A={
val res = new ResContainer(obj)
interp.beQuietDuring {
interp.bind("res", res.getClass.getCanonicalName, res)
interp.interpret("res.value = "+expression)
}
val info = obj match{
case x: AnyRef => "expected type: \n"+x.getClass.getCanonicalName+"\n"
case _ => "expected type is not an AnyRef\n"
}
res.value match{
case x: A => x
case x: AnyRef => error("unexpected result type, "+info+"result type:\n"+x.getClass.getCanonicalName)
case _ => error("unexpected result type, "+info+"result type is not an AnyRef")
}
}
}
an example of the problem:
success
Enter an integer
9/12
-->0
Enter an integer
9/0
java.lang.ArithmeticException: / by zero
at .<init>(<console>:6)
at .<clinit>(<console>)
at RequestResult$.<init>(<console>:5)
at RequestResult$.<clinit>(<console>)
at RequestResult$scala_repl_result(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.Interpreter$Request$anonfun$loadAndRun$1$anonfun$apply$17.apply(Interpreter.scala:988)
at scala.tools.nsc.Interpreter$Request$anonfun$loadAndRun$1$anonfun$apply$17.apply(Interpreter.scala:988)
at scala.util.control.Exception$Catch.apply(Exception.scala:79)
at scala...-->0
Enter an integer
9/4
-->2
Enter an integer
The ArithmeticException happened inside the interpreter, then it seems it returned nothing, so my code got the same result as previous operation, 0.
How to catch this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解释器的
interpret
方法返回一个结果值,该结果值指示代码是否可以成功解释。因此:还有两件事:您不需要为每个
eval
绑定"res"
,一旦初始化解释器就足够了。另外,请注意,有一个方法mostRecentVar
,因此您可以完全取消结果绑定。以下是 Scala 2.9 的示例(与 2.8 相同,但使用IMain
代替Interpreter
):测试:
The
interpret
method of the Interpreter returns a result value which indicates if the code could be successfully interpreted or not. Thus:Two more things: You don't need to bind
"res"
for everyeval
, it should be sufficient to do that once you initialize the interpreter. Also, note that there is a methodmostRecentVar
, so you may do away with the result binding altogether. Here is an example for Scala 2.9 (same as 2.8, but instead ofInterpreter
you useIMain
):Testing: