Custom Exception classes

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

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

On Jul 28, 2006, at 3:17 PM, Caleb C. 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 H. - [email protected] - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

On 7/29/06, Eric H. [email protected] wrote:

On Jul 28, 2006, at 3:17 PM, Caleb C. 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…

On 7/29/06, Eric H. [email protected] wrote:


Eric H. - [email protected] - 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

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 H. - [email protected] - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com

On 7/31/06, Eric H. [email protected] wrote:

p $!
$


Eric H. - [email protected] - 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 :confused:

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

Regards,
Sean

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.