exception and $!

P

Paul Rogers

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
 
J

james.d.masters

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
 
P

Paul Rogers

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

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
 
P

Phillip Gawlowski

Paul said:
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:

22. Backward compatibility is your worst enemy.

23. Backward compatibility is your users' best friend.
 
P

Paul Rogers

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

you get what you want.

--
Phillip "CynicalRyan" Gawlowskihttp://cynicalryan.110mb.com/http://clothred.rubyforge.org

Rules of Open-Source Programming:

22. Backward compatibility is your worst enemy.

23. 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.
 
J

james.d.masters

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
 
P

Paul Rogers

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

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
 
R

Robert Evans

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 Evans
http://www.junitfactory.com/
 
G

Gary Wright

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 Wright
 
R

Robert Evans

Ah! Bummer!


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 Wright
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top