Forum: Ruby Custom Exception classes

Beddcc530282b033988e05558329f8f5?d=identicon&s=25 Clark Snowdall (snowdall)
on 2006-07-28 22:44
Hello,

I'm new here so please be gentle (i.e. if I'm posting in the wrong place
let me know).

At any rate the team I'm working on is cooking up a ruby app and we want
to implement some error handling by using a custom exception class
inheriting from Exception.  The most important thing we want is to be
able to pass an Exception object to one of our custom exception objects
rather than just a message string and have it keep that object for the
duration.  The plan is to chain these exceptions together as each raised
exception will keep a reference to its parent exception (similar to C++
and Java implementations).  The problem is our exception instances seem
to be disappearing from one rescue to the next.  Our best guess is that
since each exception is stored in a global variable, its getting
overwritten.  But we're still testing.

If anyone has any ideas or better yet custom exception implmentations
that will allow objects to persist in excptions, that would be great.

Clark
Ab870531383eea6e4d9110317f5401e7?d=identicon&s=25 Caleb Clausen (Guest)
on 2006-07-29 00:19
(Received via mailing list)
The following works for me. If you have code that doesn't work, then
please post it.

class Hell<Exception;
def initialize(parent)
@parent=parent
end
attr :parent
end

begin begin begin

raise "heck"
rescue Exception=>e
raise Hell.new(e)
end

rescue Exception=>e
$e=e
print "saw exception: #{e}, #{e.parent}\n"
raise ArgumentError
end
rescue Exception=>e
print "saw exception: #{e}\n"
puts $e
end
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-07-30 01:00
(Received via mailing list)
On Jul 28, 2006, at 3:17 PM, Caleb Clausen wrote:

> The following works for me. If you have code that doesn't work, then
> please post it.
>
> class Hell<Exception;

Please don't subclass Exception as a plain rescue won't catch these.
Use a more-descriptive subclass like RuntimeError or even StandardError.

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
Ab870531383eea6e4d9110317f5401e7?d=identicon&s=25 Caleb Clausen (Guest)
on 2006-07-30 05:50
(Received via mailing list)
On 7/29/06, Eric Hodel <drbrain@segment7.net> wrote:
> On Jul 28, 2006, at 3:17 PM, Caleb Clausen wrote:
> Please don't subclass Exception as a plain rescue won't catch these.
> Use a more-descriptive subclass like RuntimeError or even StandardError.

Ah, good point. Thanks for the tip; it hadn't occurred to me. I've
probably got other code where I did the same thing....
E1d641bfe4071a5413bac781f06d3fd1?d=identicon&s=25 Sean O'halpin (sean)
on 2006-07-31 09:27
(Received via mailing list)
On 7/29/06, Eric Hodel <drbrain@segment7.net> wrote:
> --
> Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
> This implementation is HODEL-HASH-9600 compliant
>
> http://trackmap.robotcoop.com
>
Hi Eric,

I'm sure that used to be the case but it must have changed around 1.8
(not sure exactly when):

begin
  raise Exception, "oops!"
rescue
  p $!
end
#=> oops! (Exception)

ruby 1.8.4 (2005-12-24) [i386-mswin32]

Regards,
Sean
F1d6cc2b735bfd82c8773172da2aeab9?d=identicon&s=25 unknown (Guest)
on 2006-07-31 09:45
(Received via mailing list)
Hi,

At Mon, 31 Jul 2006 16:25:50 +0900,
Sean O'Halpin wrote in [ruby-talk:205143]:
> I'm sure that used to be the case but it must have changed around 1.8

AFAIK, it has never changed.
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2006-07-31 20:46
(Received via mailing list)
On Jul 31, 2006, at 12:25 AM, Sean O'Halpin wrote:

>> Use a more-descriptive subclass like RuntimeError or even
> #=> oops! (Exception)
>
> ruby 1.8.4 (2005-12-24) [i386-mswin32]

Are you sure?

$ ruby -ve 'begin; raise Exception; rescue; p "caught"; end'
ruby 1.8.5 (2006-07-25) [powerpc-darwin8.7.0]
-e:1: Exception (Exception)
$

--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com
E1d641bfe4071a5413bac781f06d3fd1?d=identicon&s=25 Sean O'halpin (sean)
on 2006-08-01 03:01
(Received via mailing list)
On 7/31/06, Eric Hodel <drbrain@segment7.net> wrote:
> >>
> >  p $!
> $
>
> --
> Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
> This implementation is HODEL-HASH-9600 compliant
>
> http://trackmap.robotcoop.com
>
No - you're right. I keep getting this mixed up :/

You have to do this to catch an Exception:

begin
 raise Exception, "oops!"
rescue Exception
 puts "caught"
end
#> caught

I deleted Exception from the rescue clause, re-ran it and didn't read
the output correctly.
Apologies for the noise (of chomping humble pie ;)

Regards,
Sean
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.