Custom Exception classes

C

Clark Snowdall

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
 
C

Caleb Clausen

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
 
E

Eric Hodel

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

Caleb Clausen

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

Sean O'Halpin

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 - (e-mail address removed) - 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
 
N

nobu

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

Eric Hodel

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

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]

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)
$
 
S

Sean O'Halpin

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.

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]

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 - (e-mail address removed) - 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,801
Messages
2,569,658
Members
45,421
Latest member
DoreenCorn

Latest Threads

Top