From: Rodrigo Rosenfeld Rosas via ruby-core Date: 2024-10-29T09:42:53-03:00 Subject: [ruby-core:119637] Behavior of raising from rescue blocks when multiple rescue blocks exist --===============4115001157247089759== Content-Type: multipart/alternative; boundary="0000000000002739f906259cedf0" --0000000000002739f906259cedf0 Content-Type: text/plain; charset="UTF-8" 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? --0000000000002739f906259cedf0 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello, I couldn't find any documentation about the sub= ject, so I thought this behavior should be probably documented.

Given the following code:

def raise= _error
=C2=A0 raise "runtime error message"
rescue =3D> = e
=C2=A0 "StandardError: #{e.message}"
rescue RuntimeError = =3D> e
=C2=A0 puts "RuntimeError raised: #{e.message}"
= =C2=A0 raise StandardError, "standard error message"
end
# same, but the order of the rescue blocks are inverted
def raise_erro= r2
=C2=A0 raise "runtime error message"
rescue RuntimeError= =3D> e
=C2=A0 puts "RuntimeError raised: #{e.message}"
= =C2=A0 raise StandardError, "standard error message"
rescue = =3D> e
=C2=A0 "StandardError: #{e.message}"
end

p= ["raise_error", raise_error ]

begin
=C2=A0 p ["ra= ise_error2", raise_error2]
rescue =3D> e
=C2=A0 puts "ra= ise_error2 raised: #{e.message}"
end

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

["raise_error", "S= tandardError: runtime error message"]

RuntimeError raise= d: runtime error message

raise_error2 raised: standard error message


In the f= irst 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 pa= rt of the specs? Is this behavior documented somewhere? Could this behavior= differ in different Ruby implementations and versions? Or can we rely on s= uch behavior?


This chapter doesn't include such case in it= s examples:

https://ruby-doc.com/docs/ProgrammingRuby/html/tut_e= xceptions.html

Or this documentation about Exceptions:

https://ru= by-doc.org/3.3.5/syntax/exceptions_rdoc.html

Is there a recommen= ded way for wrapping exceptions into a particular one and then handling tha= t exception from within the same method rescue blocks? Or is this considere= d a bad practice?


--0000000000002739f906259cedf0-- --===============4115001157247089759== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ______________________________________________ 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/ --===============4115001157247089759==--