From: Josh Cheek Date: 2013-07-05T23:08:28+09:00 Subject: How do I rescue a program exception in an at_exit block? --047d7b34419a7c3d8c04e0c43a91 Content-Type: text/plain; charset=ISO-8859-1 I was thinking about how bad I am at reading error messages, and realized it could be useful to catch the exception and write it in a way that was easier for me to find the relevant information. So, came up with this proof of concept: at_exit do error = $! if error pink, normal, red, green = "\e[35m", "\e[0m", "\e[31m", "\e[32m" puts puts "#{pink}#{error.class}" puts "#{red}#{error.message}#{normal}" puts puts error.backtrace.map { |line| line.sub(%r([^/:]*(?=:))) { |match| "#{green}#{match}#{normal}" } .sub(%r((?<=:)\d+(?=:))) { |match| "#{pink}#{match}#{normal}" } } end end The problem, though, is that $! is still set after this block runs, and so Ruby still spits out its own exception message afterwards. I don't know how to stop it from doing this. I tried setting $!, but it's a read-only variable. Tried raising and rescuing another exception, which cleared $! for the rest of that at_exit block, but it was still around in the next at_exit block, and so Ruby still printed it out. Not sure what else to try. -Josh --047d7b34419a7c3d8c04e0c43a91 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
I was thinking about how bad I am at reading error messages, and reali= zed it could be useful to catch the exception and write it in a way that wa= s easier for me to find the relevant information. So, came up with this pro= of of concept:

at_exit do
=A0 error =3D $!
=A0 if = error
=A0 =A0 pink, normal, red, green =3D "\e[35m", &q= uot;\e[0m", "\e[31m", "\e[32m"

=A0 =A0 puts
=A0 =A0 puts "#{pink}#{error.class}"
=A0 =A0 puts = "#{red}#{error.message}#{normal}"
=A0 =A0 puts
=A0 =A0 puts error.backtrace.map { |line|
=A0 =A0 =A0 line.sub(= %r([^/:]*(?=3D:))) =A0 =A0{ |match| "#{green}#{match}#{normal}" }=
=A0 =A0 =A0 =A0 =A0 .sub(%r((?<=3D:)\d+(?=3D:))) { |match| "#{= pink}#{match}#{normal}" }
=A0 =A0 }
=A0 end
<= div>end


The problem, though, is tha= t $! is still set after this block runs, and so Ruby still spits out its ow= n exception message afterwards. I don't know how to stop it from doing = this.

I tried setting $!, but it's a read-only variable. = Tried raising and rescuing another exception, which cleared $! for the rest= of that at_exit block, but it was still around in the next at_exit block, = and so Ruby still printed it out. Not sure what else to try.

-Josh
--047d7b34419a7c3d8c04e0c43a91--