ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: Rodrigo Rosenfeld Rosas via ruby-core <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: Rodrigo Rosenfeld Rosas <rr.rosas@gmail.com>
Subject: [ruby-core:119637] Behavior of raising from rescue blocks when multiple rescue blocks exist
Date: Tue, 29 Oct 2024 09:42:53 -0300	[thread overview]
Message-ID: <CAGmv+wLXtNng-RwDVCKsAVLAoHJ-Kx80L-Md_QLSiFty6izo=Q@mail.gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 1759 bytes --]

Hello, I couldn't find any documentation about the subject, so I thought
this behavior should be probably documented.

Given the following code:

def raise_error
  raise "runtime error message"
rescue => e
  "StandardError: #{e.message}"
rescue RuntimeError => e
  puts "RuntimeError raised: #{e.message}"
  raise StandardError, "standard error message"
end

# same, but the order of the rescue blocks are inverted
def raise_error2
  raise "runtime error message"
rescue RuntimeError => e
  puts "RuntimeError raised: #{e.message}"
  raise StandardError, "standard error message"
rescue => e
  "StandardError: #{e.message}"
end

p ["raise_error", raise_error ]

begin
  p ["raise_error2", raise_error2]
rescue => e
  puts "raise_error2 raised: #{e.message}"
end

When we run it, this is the output in Ruby 3.3.5:

["raise_error", "StandardError: runtime error message"]

RuntimeError raised: runtime error message

raise_error2 raised: standard error message


In the first case (raise_error), the code raised from the RuntimeError
rescue block is rescued by the StandardError block, but when inverting the
order of the rescue blocks (raise_error2) then this won't happen.

Is this part of the specs? Is this behavior documented somewhere? Could
this behavior differ in different Ruby implementations and versions? Or can
we rely on such behavior?


This chapter doesn't include such case in its examples:

https://ruby-doc.com/docs/ProgrammingRuby/html/tut_exceptions.html

Or this documentation about Exceptions:

https://ruby-doc.org/3.3.5/syntax/exceptions_rdoc.html

Is there a recommended way for wrapping exceptions into a particular one
and then handling that exception from within the same method rescue blocks?
Or is this considered a bad practice?

[-- Attachment #1.2: Type: text/html, Size: 5138 bytes --]

[-- Attachment #2: Type: text/plain, Size: 254 bytes --]

 ______________________________________________
 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/

             reply	other threads:[~2024-10-29 12:44 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-29 12:42 Rodrigo Rosenfeld Rosas via ruby-core [this message]
2024-10-29 18:07 ` [ruby-core:119639] " Austin Ziegler via ruby-core
2024-10-29 19:22   ` [ruby-core:119640] " Rodrigo Rosenfeld Rosas via ruby-core

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAGmv+wLXtNng-RwDVCKsAVLAoHJ-Kx80L-Md_QLSiFty6izo=Q@mail.gmail.com' \
    --to=ruby-core@ml.ruby-lang.org \
    --cc=rr.rosas@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).