Project

General

Profile

Feature #14684

IRB swallows exceptions

Added by RubyBugs (A Nonymous) 7 days ago. Updated 2 days ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:86528]

Description

Summary

IRB swallows exceptions, doesn't provide programatic access to raised exception in session.

Details

On Ruby 2.3.4, we find it surprising that exceptions in IRB are swallowed, and are not accessible at the two locations we checked:

  1. In the global variable for most recent exception $!
  2. In the return value of evaluating the expression _

Consider this IRB session:

def method_which_raises
  1/0
end

method_which_raises

# ZeroDivisionError: divided by 0
#   from (irb):10:in `/'
#   from (irb):10:in `method_which_raises'
#   from (irb):13
#   from /Users/marcsiegel/.rubies/ruby-2.3.4/bin/irb:11:in `<main>'

Observed

Neither _ nor $! give access to the raised exception, so that during development or debugging, one could dig further into what had occurred.

Expected

Either _ or $! would give programmatic access in IRB to an exception

Associated revisions

Revision 9fa0d005
Added by nobu (Nobuyoshi Nakada) 5 days ago

irb.rb: restore the last error

  • lib/irb.rb (eval_input): restore the last error $!, as the
    previous result. [Feature #14684]

  • lib/irb/context.rb (evaluate): add exception keyword argument
    to set the last error.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63150 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 63150
Added by nobu (Nobuyoshi Nakada) 5 days ago

irb.rb: restore the last error

  • lib/irb.rb (eval_input): restore the last error $!, as the
    previous result. [Feature #14684]

  • lib/irb/context.rb (evaluate): add exception keyword argument
    to set the last error.

History

#1 [ruby-core:86535] Updated by hsbt (Hiroshi SHIBATA) 7 days ago

  • Assignee set to keiju (Keiju Ishitsuka)
  • Status changed from Open to Assigned

#2 [ruby-core:86541] Updated by nobu (Nobuyoshi Nakada) 7 days ago

$! isn't accessible outside rescue clause, even without irb.

#3 Updated by nobu (Nobuyoshi Nakada) 7 days ago

  • Backport deleted (2.3: UNKNOWN, 2.4: UNKNOWN, 2.5: UNKNOWN)
  • ruby -v deleted (ruby 2.3.4p301 (2017-03-30 revision 58214) [x86_64-darwin15])
  • Tracker changed from Bug to Feature

#4 [ruby-core:86542] Updated by RubyBugs (A Nonymous) 6 days ago

nobu (Nobuyoshi Nakada) wrote:

$! isn't accessible outside rescue clause, even without irb.

In IRB, should $! be set, or a proxy for it be set, when the IRB eval handles an exception?

#5 Updated by nobu (Nobuyoshi Nakada) 5 days ago

  • Status changed from Assigned to Closed

Applied in changeset trunk|r63150.


irb.rb: restore the last error

  • lib/irb.rb (eval_input): restore the last error $!, as the
    previous result. [Feature #14684]

  • lib/irb/context.rb (evaluate): add exception keyword argument
    to set the last error.

#6 [ruby-core:86558] Updated by RubyBugs (A Nonymous) 2 days ago

nobu (Nobuyoshi Nakada):

Would it be possible to back-port this to Ruby 2.3, 2.4, and 2.5?

Or, would you be willing to review patches from me to back-port this?

#7 [ruby-core:86559] Updated by nobu (Nobuyoshi Nakada) 2 days ago

I changed the tracker to "Feature", as I thought it would not need to backport.
But branch maintainers may have different opinion.
It seems easy to apply the commits to the old branches.

#8 [ruby-core:86565] Updated by RubyBugs (A Nonymous) 2 days ago

Hmm. Our hope would be to have this "easy" change back-ported to 2.3, 2.4, and 2.5 -- waiting until December to have it in final 2.6 seems a long time to wait for this? Please let us know how we can help.

Also available in: Atom PDF