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

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

2 replies
Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.

I don't know if it's intentional, but the fact is that without the val you are passing a return statement that will be executed inside a method. If it's a val there's no guarantee.
If it's not intentional then way does it return from subcall and not from Function1.apply?

-------------------------------------
Trond Olsen wrote:

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

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

tolsen77
Joined: 2008-10-08,
User offline. Last seen 1 year 38 weeks ago.
Re: Re: Why NonLocalReturnException during java Method.invoke(
I  think I get it now! :) The return will return from method scope it was defined and not from within it's own block scope during it's execution. If I drop the return and just give the object it'll work fine. How do you avoid getting fooled by this in general and how common is this pitfall?

On Sat, Jul 18, 2009 at 1:17 AM, Naftoli Gugenheim <naftoligug [at] gmail [dot] com> wrote:
I don't know if it's intentional, but the fact is that without the val you are passing a return statement that will be executed inside a method. If it's a val there's no guarantee.
If it's not intentional then way does it return from subcall and not from Function1.apply?

-------------------------------------
Trond Olsen<tolsen77 [at] gmail [dot] com> wrote:

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 [at] gmail [dot] 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.
>

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Re: Why NonLocalReturnException during java Method.invoke(

By not using return. It's rarely necessary.

2009/7/18 Trond Olsen :
> I  think I get it now! :) The return will return from method scope it was
> defined and not from within it's own block scope during it's execution. If I
> drop the return and just give the object it'll work fine. How do you avoid
> getting fooled by this in general and how common is this pitfall?
>
> On Sat, Jul 18, 2009 at 1:17 AM, Naftoli Gugenheim
> wrote:
>>
>> I don't know if it's intentional, but the fact is that without the val you
>> are passing a return statement that will be executed inside a method. If
>> it's a val there's no guarantee.
>> If it's not intentional then way does it return from subcall and not from
>> Function1.apply?
>>
>> -------------------------------------
>> Trond Olsen wrote:
>>
>> 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
>>
>>
>> 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 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