This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Why NonLocalReturnException during java Method.invoke() doesn't result in a InvocationTargetException?

1 reply
tolsen77
Joined: 2008-10-08,
User offline. Last seen 1 year 38 weeks ago.
Does anyone know why this particular exception doesn't result in a InvocationTargetException with java method invokation? I can't catch it either if I add an exception wrapper around the invoke() call either.
tolsen77
Joined: 2008-10-08,
User offline. Last seen 1 year 38 weeks ago.
Re: Why NonLocalReturnException during java Method.invoke() does
My problem isn't related to reflection, but in using a return statement in a function literal that's given directly to a method call. If I define this function literal as a variable then the compiler will give an error. Should I report this as a missing error case for the compiler?

Problem example:

class A {
  def call() {
    println("Begin call")
    subcall((x: Int) => {
    println("block call")
      return x
    })
    println("End call")
  }
   
  def subcall(block: (Int) => Int): Int = {
    println("begin subcall")
    var n = 0
    for (i <- 0 until 10)
      n += block(i)
    println("end subcall")
    return n
  }
}

val a = new A
a.call()

Running this produces:
Begin call
begin subcall
block call
<execution ends without any exception>

If I assign the above function literal to a variable then the compiler gives an error:

val n = (x: Int) => { return x }

(fragment of InvokeException.scala):2: error: return outside method definition
        val n = (x: Int) => { return x }
                               ^

On Fri, Jul 17, 2009 at 6:17 PM, Trond Olsen <tolsen77@gmail.com> wrote:
Does anyone know why this particular exception doesn't result in a InvocationTargetException with java method invokation? I can't catch it either if I add an exception wrapper around the invoke() call either.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland