Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6186331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:52:13+00:00 2026-05-24T01:52:13+00:00

I am executing scala code snippets using scala.tools.nsc.Interpreter When the code snippet is correct,

  • 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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:52:15+00:00Added an answer on May 24, 2026 at 1:52 am

    The interpret method of the Interpreter returns a result value which indicates if the code could be successfully interpreted or not. Thus:

    interp.beQuietDuring {
      interp.bind("res", res.getClass.getCanonicalName, res)
      interp.interpret("res.value = "+expression)
    } match {
       case Results.Error => error( ... )
       case Results.Incomplete => error( ... )
       case Results.Success => res.value
    }
    

    Two more things: You don’t need to bind "res" for every eval, it should be sufficient to do that once you initialize the interpreter. Also, note that there is a method mostRecentVar, so you may do away with the result binding altogether. Here is an example for Scala 2.9 (same as 2.8, but instead of Interpreter you use IMain):

    import tools.nsc.interpreter.{IMain, Results}
    import sys.error
    val interp = new IMain()
    def eval( expression: String ) : AnyRef =
       interp.interpret( expression ) match {
          case Results.Error => error( "Failed" )
          case Results.Incomplete => error( "Incomplete" )
          case Results.Success => interp.valueOfTerm( interp.mostRecentVar )
            .getOrElse( error( "No result" ))
       }
    

    Testing:

    scala> val x = eval( "1 + 2" )
    res0: Int = 3
    x: AnyRef = 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to test-drive some Scala code using Specs2 and Mockito. I'm relatively new
Executing this code: mainLyr = [[CALayer layer] retain]; [mainLyr setFrame:CGRectMake(0.0,0.0,23.0,23.0)]; in debugger, I found
I'm executing stored procedures using SET FMTONLY ON, in order to emulate what our
I am executing If statement based on some vales but Nunit executes all statements
What is the Scala's way to write the following code: int i; switch(i) {
I've got a simple question for you :) I have the following Scala code,
I'm doing a code review for the Queue3 example in the Programming in Scala
When executing SubmitChanges to the DataContext after updating a couple properties with a LINQ
When executing the following (complete) SQL query on Microsoft SQL Server 2000: SELECT B.ARTIFACTTNS,
In Bash I'm executing a command and putting the result in a variable like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.