Exception and $!

the pickaxe tells me that the last exception is available in $!, but
c:>irb
irb(main):001:0> raise “Its all bad!”
RuntimeError: Its all bad!
from (irb):1
irb(main):002:0> puts $!
nil
=> nil

same thing happens from a script. Its windows
c:>ruby -v
ruby 1.8.4 (2005-12-24) [i386-mswin32]

any ideas?

Thanks
Paul

On Apr 23, 6:55 pm, Paul R. [email protected] wrote:

the pickaxe tells me that the last exception is available in $!, but
c:>irb
irb(main):001:0> raise “Its all bad!”
RuntimeError: Its all bad!
from (irb):1
irb(main):002:0> puts $!
nil

You may need to check for $! within the context of a begin/rescue/end
block; this works:

begin
raise “Its all bad!”
rescue
puts “rescuing… #{$!}”
end

BTW, I’d recommend using the following instead of $! as $! is Perl-
esque (i.e. esoteric):

begin
raise “Its all bad!”
rescue => error
puts “rescuing… #{error}”

or even better use error.backtrace for backtrace information

end

On Apr 23, 8:40 pm, [email protected] wrote:

You may need to check for $! within the context of a begin/rescue/end

begin
raise “Its all bad!”
rescue => error
puts “rescuing… #{error}”

or even better use error.backtrace for backtrace information

end

Thanks, I actually wanted to see what the last exception was out side
of a block, kind of like this
begin
raise “bad”
rescue => e
puts “#{e} was raised”
end
puts “Just to make sure, last exception was #{$!}”

Paul

On Apr 23, 9:10 pm, Phillip G. [email protected]
wrote:

If you change the above into

  1. Backward compatibility is your users’ best friend.

Once again, thanks, that works, but Im still after something slightly
different

def ex
begin
raise “oh no”
resuce =>e
puts “Exception was #{e}”
end
end

ex
puts $!

The $! seemed like a good way to do what I wanted, as i read it to be
independant of the scope the exception occured in.

On Apr 23, 8:29 pm, Paul R. [email protected] wrote:

Thanks, I actually wanted to see what the last exception was out side
of a block, kind of like this
begin
raise “bad”
rescue => e
puts “#{e} was raised”
end
puts “Just to make sure, last exception was #{$!}”

If you don’t have your heart set on using $!, then just put the
contents into a variable (code not tested):

last_error = nil
begin
raise “bad”
rescue => e
last_error = e
end

puts “An error was encountered: #{e}” if e

Paul R. wrote:

Thanks, I actually wanted to see what the last exception was out side
of a block, kind of like this
begin
raise “bad”
rescue => e
puts “#{e} was raised”
end
puts “Just to make sure, last exception was #{$!}”

If you change the above into
puts “Just to make sure, last exception was #{e}”

you get what you want.


Phillip “CynicalRyan” Gawlowski
http://cynicalryan.110mb.com/
http://clothred.rubyforge.org

Rules of Open-Source Programming:

  1. Backward compatibility is your worst enemy.

  2. Backward compatibility is your users’ best friend.

If I understand what you are trying to do (refactor a bunch of
exception handling code) then maybe overriding the #rescue method in
general for your session so that it always print it’s exception and
stacktrace, would give you the information you want.

Bob E.
http://www.junitfactory.com/

On Apr 24, 2007, at 12:04 PM, Robert E. wrote:

If I understand what you are trying to do (refactor a bunch of
exception handling code) then maybe overriding the #rescue method
in general for your session so that it always print it’s exception
and stacktrace, would give you the information you want.

rescue is a keyword, not a method, and so can’t be redefined.

I tried overriding Exception.new, but that didn’t work.
I think Ruby cheats and bypasses Exception.new in some cases.

Gary W.

On Apr 23, 9:42 pm, [email protected] wrote:

puts “An error was encountered: #{e}” if e
The reason I have to do this, and why $! was so attractive is that I
have many rescue blocks scattered throughout the code, which Im trying
to fix up, but I dont want to do them all at once, so just seeing the
last exception, was going to be helpful. I have another work around
that isnt as nice as $! appeared to be, but it does enough for what I
need

Thanks for all your help
Paul

Ah! Bummer!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs