From: "tikkss (Tsutomu Katsube) via ruby-core" Date: 2026-04-10T21:34:41+00:00 Subject: [ruby-core:125235] [Ruby Bug#21991] `$!` stays as the first exception in Ruby Box Issue #21991 has been reported by tikkss (Tsutomu Katsube). ---------------------------------------- Bug #21991: `$!` stays as the first exception in Ruby Box https://bugs.ruby-lang.org/issues/21991 * Author: tikkss (Tsutomu Katsube) * Status: Open * ruby -v: ruby 4.0.2 (2026-03-17 revision d3da9fec82) +PRISM [x86_64-darwin24] * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- Description --- When Ruby Box is enabled (`RUBY_BOX=1`), `$!` inside `rescue` does not change after the first exception. `$!` is expected to show the exception for each `rescue` block, but it always shows the first one. Steps to reproduce --- ```ruby # test.rb begin raise "First error" rescue pp $! end begin raise "Second error" rescue pp $! end begin raise "Third error" rescue pp $! end ``` Expected result --- ```console $ RUBY_BOX=1 ruby test.rb ruby: warning: Ruby::Box is experimental, and the behavior may change in the future! See https://docs.ruby-lang.org/en/4.0/Ruby/Box.html for known issues, etc. # # # ``` Actual result --- ```console $ RUBY_BOX=1 ruby test.rb ruby: warning: Ruby::Box is experimental, and the behavior may change in the future! See https://docs.ruby-lang.org/en/4.0/Ruby/Box.html for known issues, etc. # # # ``` Additional information --- This issue does not reproduce when `RUBY_BOX=1` is not set: ```console $ ruby test.rb # # # ``` Also, this issue does not reproduce when the exception object is captured explicitly in the `rescue` clause: ```ruby # test.rb begin raise "First error" rescue => e pp e end begin raise "Second error" rescue => e pp e end begin raise "Third error" rescue => e pp e end ``` ```console $ RUBY_BOX=1 ruby test.rb ruby: warning: Ruby::Box is experimental, and the behavior may change in the future! See https://docs.ruby-lang.org/en/4.0/Ruby/Box.html for known issues, etc. # # # ``` -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/