[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

exception and $!

Paul Rogers

4/24/2007 1:56:00 AM

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

9 Answers

james.d.masters

4/24/2007 2:40:00 AM

0

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

4/24/2007 3:00:00 AM

0

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

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

Phillip Gawlowski

4/24/2007 3:10:00 AM

0

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....
http://clothred.rub...

Rules of Open-Source Programming:

22. Backward compatibility is your worst enemy.

23. Backward compatibility is your users' best friend.

Paul Rogers

4/24/2007 3:29:00 AM

0

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.rub...
>
> 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.

james.d.masters

4/24/2007 3:43:00 AM

0

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

Paul Rogers

4/24/2007 2:50:00 PM

0

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

Robert Evans

4/24/2007 4:04:00 PM

0

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.junitfa...

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


Gary Wright

4/24/2007 6:38:00 PM

0


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




Robert Evans

4/24/2007 11:28:00 PM

0

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