From: Matthew Kerwin Date: 2014-08-06T08:28:10+10:00 Subject: [ruby-core:64211] Re: Asking for clarification for exception handling usage --001a113b4eccd9351b04ffe95e60 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable To answer the first half of your question, the "cause" is an underlying exception (inherited from $!), so you can use it to chain errors. For example: begin begin raise 'foo' rescue =3D> foo raise 'bar' end rescue =3D> bar end bar #=3D> # bar.cause =3D=3D foo #=3D> true As to the second half, the third parameter to #raise is an optionally-overridden backtrace: begin raise RuntimeError, 'baz', %w[a b c] rescue =3D> ex end ex.backtrace #=3D> ["a", "b", "c"] Passing an exception instance to #raise does the following: begin begin raise 'foo' rescue =3D> foo raise foo # re-raise end rescue =3D> bar end foo.__id__ # =3D> 70351312183760 bar.__id__ # =3D> 70351312183760 begin begin raise 'foo' rescue =3D> foo raise foo, 'bar' # copy and raise end rescue =3D> bar end foo.__id__ # =3D> 70071384070800 bar.__id__ # =3D> 70071384070620 bar.cause =3D=3D foo # =3D> true In the first case the backtrace for both foo and bar lists line 3; in the second they have line 3 and 5 respectively. I've never heard of an exception's #cause returning anything but an Exception instance or nil. =E2=80=8B On 6 August 2014 06:01, Rodrigo Rosenfeld Rosas wrote: > I've created a ticket for that but didn't get any feedback so I decided t= o > try asking in another way: > > https://bugs.ruby-lang.org/issues/10110 > > I'd like to understand what is Exception#cause and what exactly does the > third parameter of Kernel#raise. > > I've seen a code calling raise exception_instance, message rather than > passing the exception class and the cause for the handled exception was a > string rather than an Exception. I don't even know if this is a bug and i= f > raise should fail if you pass an exception instance with a message as I > don't really understand how exception handling works in Ruby and what to > expect for Exception#cause and how to properly wrap an exception for > logging purposes... > > If you could at least explain what Exception#cause is supposed to carry o= n > and what the third argument of Kernel#raise does exactly by the specs I'd > be a lot grateful. > > Thanks in advance, > Rodrigo. > > --=20 Matthew Kerwin http://matthew.kerwin.net.au/ --001a113b4eccd9351b04ffe95e60 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
= To answer the first half of your = question, the "cause" is an underlying exception (inherited from = $!), so you can use it to chain errors. For example:

=C2=A0 begin
=C2=A0 =C2=A0 begin=
=C2=A0 =C2=A0 =C2=A0 raise &#= 39;foo'
=C2=A0 =C2=A0 rescue =3D> foo
=C2=A0 =C2=A0 =C2=A0 raise '= bar'
=C2=A0 =C2=A0 end
=C2=A0 rescue =3D> bar
=C2=A0 end
=C2=A0 bar #=3D> #<RuntimeError: bar>
=C2=A0 bar.cause =3D=3D foo #=3D>= true

As to the second half, the third paramete= r to #raise is an optionally-overridden backtrace:

=C2=A0 begin
= =C2=A0 =C2=A0 raise RuntimeError, 'baz', %w[a b c]
=C2=A0 rescue =3D> ex<= /div>
=C2=A0 end
=C2=A0 ex.backtrace #=3D> ["a", "b= ", "c"]

Passing an exception instance to #r= aise does the following:

=C2=A0 begin
=C2=A0 =C2=A0 begin
=C2=A0 =C2=A0 =C2=A0 raise 'foo'
=C2=A0 =C2=A0 rescue =3D> foo
=C2=A0 =C2=A0 =C2=A0 raise foo # re-raise
=C2=A0 =C2=A0 end
=C2=A0 rescue =3D> bar
=C2=A0 end
=C2=A0 foo.__id__=C2=A0# =3D> 70351312183760
=C2=A0 bar.__id__=C2=A0# =3D> 70351312183760

=C2=A0 begin
=C2=A0 =C2=A0 begin
=C2=A0 =C2=A0 =C2=A0 raise 'foo'
<= div class=3D"gmail_default" style=3D"color:rgb(7,55,99)">=C2=A0 =C2=A0 rescue =3D> foo
=C2=A0 =C2=A0 =C2=A0 raise foo, 'bar' # copy a= nd raise
=C2=A0 =C2=A0 end
=C2=A0 rescue =3D> bar
=C2=A0 end
=C2=A0 foo.__id__=C2=A0# =3D&g= t;=C2=A070071384070800
=C2=A0 bar.__id__=C2=A0# =3D>=C2=A07= 0071384070620
=C2=A0 bar.cause =3D=3D foo # =3D> true


In the first case the backtrace for both= foo and bar lists line 3; in the second they have line 3 and= 5 respectively. I've never heard of an exception's #cause returnin= g anything but an Exception instance or nil.
=E2=80=8B

On 6 August 2014 06:= 01, Rodrigo Rosenfeld Rosas <rr.rosas@gmail.com> wrote:
I've created a ticket for that but didn't get any = feedback so I decided to try asking in another way:

https= ://bugs.ruby-lang.org/issues/10110

I'd like to understand what is Exception#cause and what exactly does th= e third parameter of Kernel#raise.

I've seen a code calling raise exception_instance, message rather than = passing the exception class and the cause for the handled exception was a s= tring rather than an Exception. I don't even know if this is a bug and = if raise should fail if you pass an exception instance with a message as I = don't really understand how exception handling works in Ruby and what t= o expect for Exception#cause and how to properly wrap an exception for logg= ing purposes...

If you could at least explain what Exception#cause is supposed to carry on = and what the third argument of Kernel#raise does exactly by the specs I'= ;d be a lot grateful.

Thanks in advance,
Rodrigo.




--
=C2=A0 Matthew Kerwin
=C2=A0 http://matthew.kerwin.net.au/
--001a113b4eccd9351b04ffe95e60--