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

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, 6:55 pm, Paul Rogers <pmr16...@gmail.com> 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

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, 8:40 pm, james.d.mast...@gmail.com wrote:

On Apr 23, 6:55 pm, Paul Rogers <pmr16...@gmail.com> 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

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

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.

···

On Apr 23, 9:10 pm, Phillip Gawlowski <cmdjackr...@googlemail.com> wrote:

Paul Rogers 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" 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.

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

···

On Apr 23, 8:29 pm, Paul Rogers <pmr16...@gmail.com> 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 #{$!}"

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

···

On Apr 23, 9:42 pm, james.d.mast...@gmail.com wrote:

On Apr 23, 8:29 pm, Paul Rogers <pmr16...@gmail.com> 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

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

···

On Apr 24, 2007, at 7:50 AM, Paul Rogers wrote:

On Apr 23, 9:42 pm, james.d.mast...@gmail.com wrote:

On Apr 23, 8:29 pm, Paul Rogers <pmr16...@gmail.com> 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

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

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

···

On Apr 24, 2007, at 12:04 PM, Robert Evans 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.

Ah! Bummer!

···

On Apr 24, 2007, at 11:38 AM, Gary Wright wrote:

On Apr 24, 2007, at 12:04 PM, Robert Evans 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 Wright