# Entering try-catch with non-empty stack

**URL:** <https://www.ruby-forum.com/t/entering-try-catch-with-non-empty-stack/116074>\
**Category:** IronRuby\
**Created:** [October 2, 2007, 3:25am UTC](https://www.ruby-forum.com/t/entering-try-catch-with-non-empty-stack/116074 "2007-10-02T03:25:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sanghyeon\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@Sanghyeon\_S](https://www.ruby-forum.com/u/Sanghyeon_S)\
**Post date:** [October 2, 2007, 3:25am UTC](https://www.ruby-forum.com/t/entering-try-catch-with-non-empty-stack/116074/1 "2007-10-02T03:25:13Z")

</div>

As a recent thread pointed out, this list so far have been more like  
ironruby-talk than ironruby-core. 🙂

So, to start the discussion, I have a question. In  
tests/ironruby/Runtime/Block/test\_closure, I see this comment:

# TODO:

# assert\_equal(foo(&p)) were replaced by x = foo(&p); assert\_equal(x)

# known DLR bug (entering try-catch with non-empty stack)

This looks like a same problem JRuby encountered and solved. As  
Charles is on the list, he could provide some insight. 🙂

A good introduction to this issue from JRuby point of view was written  
by Ola B.:

> **[The pain of compiling try-catch](http://ola-bini.blogspot.com/2007/08/pain-of-compiling-try-catch.html)**
>
> I've been spending some time trying to implement a compiler for the defined?-feature of Ruby. If you haven't seen it, be happy. It's quite a...

---

<div class="post-metadata">

**Author:** ![Sanghyeon\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@Sanghyeon\_S](https://www.ruby-forum.com/u/Sanghyeon_S)\
**Post date:** [October 2, 2007, 3:50am UTC](https://www.ruby-forum.com/t/entering-try-catch-with-non-empty-stack/116074/2 "2007-10-02T03:50:23Z")

</div>

Sanghyeon S. wrote:

> This looks like a same problem JRuby encountered and solved. As  
> Charles is on the list, he could provide some insight. 🙂
> 
> A good introduction to this issue from JRuby point of view was written  
> by Ola B.:  
> [Ola Bini: Programming Language Synchronicity: The pain of compiling try-catch](http://ola-bini.blogspot.com/2007/08/pain-of-compiling-try-catch.html)

I don’t think this qualifies as a bug; both the JVM and the CLR prepare  
a new activation (and an empty stack) when you enter an  
exception-handling section. For a language like Ruby, where you can, for  
example, prepare arguments to a method call by doing a loop or  
begin/rescue that ultimately return a value, you need to be able to  
maintain the stack. XRuby’s approach is to save the stack, but I don’t  
believe they support all such situations. JRuby generates a new method  
for such situations, to allow the original activation maintain the same  
stack state. In theory both approaches work, but they both have their  
own complications. Currently we create what’s called “synthetic methods”  
in the JVM to contain the body of e.g. a begin block attached to a  
rescue block. We may also have to do this for while blocks when there’s  
a chance that stack state must be maintained. I think we can determine  
statically when we need to create synthetic methods and when we can’t.

- Charlie

---

<div class="post-metadata">

**Author:** ![Sanghyeon\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@Sanghyeon\_S](https://www.ruby-forum.com/u/Sanghyeon_S)\
**Post date:** [October 2, 2007, 5:29am UTC](https://www.ruby-forum.com/t/entering-try-catch-with-non-empty-stack/116074/3 "2007-10-02T05:29:29Z")

</div>

We are thinking about an AST transformation that would push forward  
expression evaluations, store results to temp variables and reload them  
where necessary. But we didn’t conclude on this yet. Introducing  
additional method calls is IMO not necessary.

Tomas

---

<div class="post-metadata">

**Author:** ![Sanghyeon\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@Sanghyeon\_S](https://www.ruby-forum.com/u/Sanghyeon_S)\
**Post date:** [October 2, 2007, 5:45am UTC](https://www.ruby-forum.com/t/entering-try-catch-with-non-empty-stack/116074/4 "2007-10-02T05:45:54Z")

</div>

Tomas M. wrote:

> We are thinking about an AST transformation that would push forward expression evaluations, store results to temp variables and reload them where necessary. But we didn’t conclude on this yet. Introducing additional method calls is IMO not necessary.

To me the complexity of this approach is for many nested sets of such  
expressions:

x = 1  
x += begin  
x += begin  
x += begin  
x += begin  
raise  
rescue  
1  
end  
end  
end  
end

This is somewhat contrived, but there are many cases of at least a  
couple nested expressions with exception handling that may happen during  
stack-building. You would need to continue pushing down and storing  
values on the stack at each level, restoring as you climb back out.

FWIW, the above works ok in JRuby right now, but if the begin/rescue is  
replaced with while loops it fails to compile. I will likely have to use  
the same approach for while loops, since they also implicitly handle  
LocalJumpError exceptions.

- Charlie
