# Custom Exception classes

**URL:** <https://www.ruby-forum.com/t/custom-exception-classes/100443>\
**Category:** Ruby\
**Created:** [July 28, 2006, 10:44pm UTC](https://www.ruby-forum.com/t/custom-exception-classes/100443 "2006-07-28T22:44:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![snowdall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@snowdall](https://www.ruby-forum.com/u/snowdall)\
**Post date:** [July 28, 2006, 10:44pm UTC](https://www.ruby-forum.com/t/custom-exception-classes/100443/1 "2006-07-28T22:44:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![snowdall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@snowdall](https://www.ruby-forum.com/u/snowdall)\
**Post date:** [July 29, 2006, 12:19am UTC](https://www.ruby-forum.com/t/custom-exception-classes/100443/2 "2006-07-29T00:19:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![snowdall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@snowdall](https://www.ruby-forum.com/u/snowdall)\
**Post date:** [July 30, 2006, 1:00am UTC](https://www.ruby-forum.com/t/custom-exception-classes/100443/3 "2006-07-30T01:00:11Z")

</div>

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. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![snowdall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@snowdall](https://www.ruby-forum.com/u/snowdall)\
**Post date:** [July 30, 2006, 5:50am UTC](https://www.ruby-forum.com/t/custom-exception-classes/100443/4 "2006-07-30T05:50:00Z")

</div>

On 7/29/06, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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…

---

<div class="post-metadata">

**Author:** ![snowdall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@snowdall](https://www.ruby-forum.com/u/snowdall)\
**Post date:** [July 31, 2006, 9:27am UTC](https://www.ruby-forum.com/t/custom-exception-classes/100443/5 "2006-07-31T09:27:36Z")

</div>

On 7/29/06, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
> This implementation is HODEL-HASH-9600 compliant
> 
> [http://trackmap.robotcoop.com](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

---

<div class="post-metadata">

**Author:** ![snowdall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@snowdall](https://www.ruby-forum.com/u/snowdall)\
**Post date:** [July 31, 2006, 8:46pm UTC](https://www.ruby-forum.com/t/custom-exception-classes/100443/6 "2006-07-31T20:46:00Z")

</div>

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. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![snowdall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@snowdall](https://www.ruby-forum.com/u/snowdall)\
**Post date:** [August 1, 2006, 3:01am UTC](https://www.ruby-forum.com/t/custom-exception-classes/100443/7 "2006-08-01T03:01:37Z")

</div>

On 7/31/06, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > 
> > 
> > p $!  
> > $
> 
> –  
> Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
> This implementation is HODEL-HASH-9600 compliant
> 
> [http://trackmap.robotcoop.com](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

---

<div class="post-metadata">

**Author:** ![snowdall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/45deac/32.png) [@snowdall](https://www.ruby-forum.com/u/snowdall)\
**Post date:** [July 31, 2006, 9:45am UTC](https://www.ruby-forum.com/t/custom-exception-classes/100443/8 "2006-07-31T09:45:57Z")

</div>

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.
