How do I get the type (class) of exception thrown by any given peice of
code ?
I was playing with sockets and wanted to rescue the “<Errno::E10061: No
connection could be made…” exception.
My dumb way was to try and guess it by looking at the heirarchy tree on Page
93 of PickAxe. After a couple of wrong guesses finally found out it was
RuntimeError. But what if it was none of those (like the TimeoutError)?
Surely, there is a smart (Ruby) way to do this ?
– shanko
Maybe something like this helps you?
irb(main):003:0> class MyError < StandardError
irb(main):004:1> end
=> nil
irb(main):005:0> def meth
irb(main):006:1> raise(MyError, ‘Error here’)
irb(main):007:1> end
=> nil
irb(main):009:0> begin
irb(main):010:1* meth
irb(main):011:1> rescue StandardError => e
irb(main):012:1> puts e.type.to_s
irb(main):013:1> end
MyError
=> nil
Signed,
Holden Glova
···
On Sat, 18 Jan 2003 17:49, Shashank Date wrote:
How do I get the type (class) of exception thrown by any given peice of
code ?
I was playing with sockets and wanted to rescue the “<Errno::E10061: No
connection could be made…” exception.
My dumb way was to try and guess it by looking at the heirarchy tree on
Page 93 of PickAxe. After a couple of wrong guesses finally found out it
was RuntimeError. But what if it was none of those (like the TimeoutError)?
Surely, there is a smart (Ruby) way to do this ?
– shanko
“Holden Glova” dsafari@paradise.net.nz wrote in message
How do I get the type (class) of exception thrown by any given peice of
code ?
irb(main):011:1> rescue StandardError => e
irb(main):012:1> puts e.type.to_s
irb(main):013:1> end
But of course ! Duh !!
I modified it slightly as:
rescue Exception => e
to catch every possible exception.
Signed,
Holden Glova
Thanks a lot …
– shanko
···
On Sat, 18 Jan 2003 17:49, Shashank Date wrote: