# Programmatically and dynamically catching exceptions

**URL:** <https://rubytalk.org/t/programmatically-and-dynamically-catching-exceptions/14562>\
**Category:** ruby-talk\
**Created:** [3 November 2004 02:20 UTC](https://rubytalk.org/t/programmatically-and-dynamically-catching-exceptions/14562 "2004-11-03T02:20:20Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [3 November 2004 02:20 UTC](https://rubytalk.org/t/programmatically-and-dynamically-catching-exceptions/14562/1 "2004-11-03T02:20:20Z")

</div>

Allright here was my idea which seems to have been shattered by the  
realities of Ruby.

I was thinking of a class called Try. You would do something like

class Try  
...  
end

tryblock = Try.new { #This block would normally be wrapped in a begin...rescue  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

tryblock.add\_exhandler(:SomeException) { |ex| some\_code }

tryblock.execute #or possibly tryblock.call ?

Basically the idea was going to be you could subclass Try and provide  
default sensible handling of your Exceptions (if you were writing a  
library for instance) . Users could then then wrap it in a  
begin...rescue block to catch other exceptions or use add\_exhandler to  
overide the default handler. (Possibly provide a way to get the old  
handler and and use it in the new).

my idea was going to be that I could do something like

begin  
&nbsp;&nbsp;&nbsp;user\_proc.call  
[SomeEx1, SomeEx2, etc..].each do |ex|  
rescue ex =\> e  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handler\_hash[ex].call(e)  
end

Unfortunately the syntax doesn;t seem to allow for this. Am I missing  
something? is this just a really bad idea?

---

<div class="post-metadata">

**Author:** ![Yohanes\_Santoso1](https://avatars.discourse-cdn.com/v4/letter/y/ccd318/32.png) [@Yohanes\_Santoso1](https://rubytalk.org/u/Yohanes_Santoso1)\
**Post date:** [3 November 2004 04:20 UTC](https://rubytalk.org/t/programmatically-and-dynamically-catching-exceptions/14562/2 "2004-11-03T04:20:28Z")

</div>

Logan Capaldo \<logancapaldo@gmail.com\> writes:

> Allright here was my idea which seems to have been shattered by the  
> realities of Ruby.
> 
> I was thinking of a class called Try. You would do something like
> 
> class Try  
> ...  
> end
> 
> tryblock = Try.new { #This block would normally be wrapped in a begin...rescue  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
> 
> tryblock.add\_exhandler(:SomeException) { |ex| some\_code }
> 
> tryblock.execute #or possibly tryblock.call ?
> 
> Basically the idea was going to be you could subclass Try and provide  
> default sensible handling of your Exceptions (if you were writing a  
> library for instance) . Users could then then wrap it in a  
> begin...rescue block to catch other exceptions or use add\_exhandler to  
> overide the default handler. (Possibly provide a way to get the old  
> handler and and use it in the new).
> 
> my idea was going to be that I could do something like

class Try  
&nbsp;&nbsp;def execute  
&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user\_proc.call(\*user\_proc\_args)  
&nbsp;&nbsp;&nbsp;&nbsp;rescue Error =\> e  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elt = (exhandlers.select{|obj| e.class == obj.exception\_class})[0]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if elt  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elt.exception\_handler.call(e)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise e  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

YS.

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [3 November 2004 05:34 UTC](https://rubytalk.org/t/programmatically-and-dynamically-catching-exceptions/14562/3 "2004-11-03T05:34:22Z")

</div>

Thanks thats quite neat.

> **···**
>
> On Wed, 3 Nov 2004 13:20:28 +0900, Yohanes Santoso \<ysantoso-rubytalk@dessyku.is-a-geek.org\> wrote:
> 
> > Logan Capaldo \<logancapaldo@gmail.com\> writes:
> > 
> > \> Allright here was my idea which seems to have been shattered by the  
> > \> realities of Ruby.  
> > \>  
> > \> I was thinking of a class called Try. You would do something like  
> > \>  
> > \> class Try  
> > \> ...  
> > \> end  
> > \>  
> > \> tryblock = Try.new { #This block would normally be wrapped in a begin...rescue  
> > \> }  
> > \>  
> > \> tryblock.add\_exhandler(:SomeException) { |ex| some\_code }  
> > \>  
> > \> tryblock.execute #or possibly tryblock.call ?  
> > \>  
> > \> Basically the idea was going to be you could subclass Try and provide  
> > \> default sensible handling of your Exceptions (if you were writing a  
> > \> library for instance) . Users could then then wrap it in a  
> > \> begin...rescue block to catch other exceptions or use add\_exhandler to  
> > \> overide the default handler. (Possibly provide a way to get the old  
> > \> handler and and use it in the new).  
> > \>  
> > \> my idea was going to be that I could do something like  
> > \>
> > 
> > class Try  
> > &nbsp;&nbsp;def execute  
> > &nbsp;&nbsp;&nbsp;&nbsp;begin  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user\_proc.call(\*user\_proc\_args)  
> > &nbsp;&nbsp;&nbsp;&nbsp;rescue Error =\> e  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elt = (exhandlers.select{|obj| e.class == obj.exception\_class})[0]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if elt  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elt.exception\_handler.call(e)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise e  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > end
> > 
> > YS.

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [3 November 2004 10:38 UTC](https://rubytalk.org/t/programmatically-and-dynamically-catching-exceptions/14562/4 "2004-11-03T10:38:48Z")

</div>

"Logan Capaldo" \<logancapaldo@gmail.com\> schrieb im Newsbeitrag  
news:4521f6680411022134451a94d1@mail.gmail.com...

> Thanks thats quite neat.
> 
> \> Logan Capaldo \<logancapaldo@gmail.com\> writes:  
> \>  
> \>  
> \>  
> \> \> Allright here was my idea which seems to have been shattered by the  
> \> \> realities of Ruby.  
> \> \>  
> \> \> I was thinking of a class called Try. You would do something like  
> \> \>  
> \> \> class Try  
> \> \> ...  
> \> \> end  
> \> \>  
> \> \> tryblock = Try.new { #This block would normally be wrapped in a

begin...rescue

> \> \> }  
> \> \>  
> \> \> tryblock.add\_exhandler(:SomeException) { |ex| some\_code }  
> \> \>  
> \> \> tryblock.execute #or possibly tryblock.call ?  
> \> \>  
> \> \> Basically the idea was going to be you could subclass Try and

provide

> \> \> default sensible handling of your Exceptions (if you were writing a  
> \> \> library for instance) . Users could then then wrap it in a  
> \> \> begin...rescue block to catch other exceptions or use add\_exhandler

to

> \> \> overide the default handler. (Possibly provide a way to get the old  
> \> \> handler and and use it in the new).  
> \> \>  
> \> \> my idea was going to be that I could do something like  
> \> \>  
> \>  
> \> class Try  
> \> def execute  
> \> begin  
> \> user\_proc.call(\*user\_proc\_args)  
> \> rescue Error =\> e  
> \> elt = (exhandlers.select{|obj| e.class ==

obj.exception\_class})[0]

> \> if elt  
> \> elt.exception\_handler.call(e)  
> \> else  
> \> raise e  
> \> end  
> \> end  
> \> end  
> \> end

We can save three more lines 🙂

class Try  
&nbsp;&nbsp;def execute  
&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user\_proc.call(\*user\_proc\_args)  
&nbsp;&nbsp;&nbsp;&nbsp;rescue Error =\> e  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elt = (exhandlers.select{|obj| e.class == obj.exception\_class})[0]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise unless elt  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elt.exception\_handler.call(e)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> > On Wed, 3 Nov 2004 13:20:28 +0900, Yohanes Santoso \> \<ysantoso-rubytalk@dessyku.is-a-geek.org\> wrote:

---

<div class="post-metadata">

**Author:** ![Tim\_Sutherland1](https://avatars.discourse-cdn.com/v4/letter/t/8e7dd6/32.png) [@Tim\_Sutherland1](https://rubytalk.org/u/Tim_Sutherland1)\
**Post date:** [3 November 2004 10:43 UTC](https://rubytalk.org/t/programmatically-and-dynamically-catching-exceptions/14562/5 "2004-11-03T10:43:50Z")

</div>

As long as we're saving lines...

class Try  
&nbsp;&nbsp;def execute  
&nbsp;&nbsp;&nbsp;&nbsp;user\_proc.call(\*user\_proc\_args)  
&nbsp;&nbsp;rescue Error =\> e  
&nbsp;&nbsp;&nbsp;&nbsp;elt = (exhandlers.select{|obj| e.class == obj.exception\_class})[0] or raise  
&nbsp;&nbsp;&nbsp;&nbsp;elt.exception\_handler.call(e)  
&nbsp;&nbsp;end  
end

> **···**
>
> In article \<2urqndF2cbspnU1@uni-berlin.de\>, Robert Klemme wrote:
> 
> > "Logan Capaldo" \<logancapaldo@gmail.com\> schrieb im Newsbeitrag  
> > news:4521f6680411022134451a94d1@mail.gmail.com...
> > 
> > > Thanks thats quite neat.
> > > 
> > > On Wed, 3 Nov 2004 13:20:28 +0900, Yohanes Santoso \>\> \<ysantoso-rubytalk@dessyku.is-a-geek.org\> wrote:  
> > > \> Logan Capaldo \<logancapaldo@gmail.com\> writes:  
> > > \>  
> > > \>  
> > > \>  
> > > \> \> Allright here was my idea which seems to have been shattered by the  
> > > \> \> realities of Ruby.  
> > > \> \>  
> > > \> \> I was thinking of a class called Try. You would do something like  
> > > \> \>  
> > > \> \> class Try  
> > > \> \> ...  
> > > \> \> end  
> > > \> \>  
> > > \> \> tryblock = Try.new { #This block would normally be wrapped in a
> > 
> > begin...rescue
> > 
> > > \> \> }  
> > > \> \>  
> > > \> \> tryblock.add\_exhandler(:SomeException) { |ex| some\_code }  
> > > \> \>  
> > > \> \> tryblock.execute #or possibly tryblock.call ?  
> > > \> \>  
> > > \> \> Basically the idea was going to be you could subclass Try and
> > 
> > provide
> > 
> > > \> \> default sensible handling of your Exceptions (if you were writing a  
> > > \> \> library for instance) . Users could then then wrap it in a  
> > > \> \> begin...rescue block to catch other exceptions or use add\_exhandler
> > 
> > to
> > 
> > > \> \> overide the default handler. (Possibly provide a way to get the old  
> > > \> \> handler and and use it in the new).  
> > > \> \>  
> > > \> \> my idea was going to be that I could do something like  
> > > \> \>  
> > > \>  
> > > \> class Try  
> > > \> def execute  
> > > \> begin  
> > > \> user\_proc.call(\*user\_proc\_args)  
> > > \> rescue Error =\> e  
> > > \> elt = (exhandlers.select{|obj| e.class ==
> > 
> > obj.exception\_class})[0]
> > 
> > > \> if elt  
> > > \> elt.exception\_handler.call(e)  
> > > \> else  
> > > \> raise e  
> > > \> end  
> > > \> end  
> > > \> end  
> > > \> end
> > 
> > We can save three more lines 🙂
> > 
> > class Try  
> > def execute  
> > &nbsp;&nbsp;&nbsp;begin  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user\_proc.call(\*user\_proc\_args)  
> > &nbsp;&nbsp;&nbsp;rescue Error =\> e  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elt = (exhandlers.select{|obj| e.class == obj.exception\_class})[0]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise unless elt  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elt.exception\_handler.call(e)  
> > &nbsp;&nbsp;&nbsp;end  
> > end  
> > end

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [3 November 2004 13:03 UTC](https://rubytalk.org/t/programmatically-and-dynamically-catching-exceptions/14562/6 "2004-11-03T13:03:51Z")

</div>

"Tim Sutherland" \<timsuth@ihug.co.nz\> schrieb im Newsbeitrag  
news:slrncohddq.pa3.timsuth@europa.zone...

> \>  
> \>"Logan Capaldo" \<logancapaldo@gmail.com\> schrieb im Newsbeitrag  
> \>news:4521f6680411022134451a94d1@mail.gmail.com...  
> \>\> Thanks thats quite neat.  
> \>\>  
> \>\> \> Logan Capaldo \<logancapaldo@gmail.com\> writes:  
> \>\> \>  
> \>\> \>  
> \>\> \>  
> \>\> \> \> Allright here was my idea which seems to have been shattered by

the

> \>\> \> \> realities of Ruby.  
> \>\> \> \>  
> \>\> \> \> I was thinking of a class called Try. You would do something like  
> \>\> \> \>  
> \>\> \> \> class Try  
> \>\> \> \> ...  
> \>\> \> \> end  
> \>\> \> \>  
> \>\> \> \> tryblock = Try.new { #This block would normally be wrapped in a  
> \>begin...rescue  
> \>\> \> \> }  
> \>\> \> \>  
> \>\> \> \> tryblock.add\_exhandler(:SomeException) { |ex| some\_code }  
> \>\> \> \>  
> \>\> \> \> tryblock.execute #or possibly tryblock.call ?  
> \>\> \> \>  
> \>\> \> \> Basically the idea was going to be you could subclass Try and  
> \>provide  
> \>\> \> \> default sensible handling of your Exceptions (if you were writing

a

> \>\> \> \> library for instance) . Users could then then wrap it in a  
> \>\> \> \> begin...rescue block to catch other exceptions or use

add\_exhandler

> \>to  
> \>\> \> \> overide the default handler. (Possibly provide a way to get the

old

> \>\> \> \> handler and and use it in the new).  
> \>\> \> \>  
> \>\> \> \> my idea was going to be that I could do something like  
> \>\> \> \>  
> \>\> \>  
> \>\> \> class Try  
> \>\> \> def execute  
> \>\> \> begin  
> \>\> \> user\_proc.call(\*user\_proc\_args)  
> \>\> \> rescue Error =\> e  
> \>\> \> elt = (exhandlers.select{|obj| e.class ==  
> \>obj.exception\_class})[0]  
> \>\> \> if elt  
> \>\> \> elt.exception\_handler.call(e)  
> \>\> \> else  
> \>\> \> raise e  
> \>\> \> end  
> \>\> \> end  
> \>\> \> end  
> \>\> \> end  
> \>  
> \>We can save three more lines 🙂  
> \>  
> \>class Try  
> \> def execute  
> \> begin  
> \> user\_proc.call(\*user\_proc\_args)  
> \> rescue Error =\> e  
> \> elt = (exhandlers.select{|obj| e.class ==

obj.exception\_class})[0]

> \> raise unless elt  
> \> elt.exception\_handler.call(e)  
> \> end  
> \> end  
> \>end
> 
> As long as we're saving lines...
> 
> class Try  
> &nbsp;&nbsp;def execute  
> &nbsp;&nbsp;&nbsp;&nbsp;user\_proc.call(\*user\_proc\_args)  
> &nbsp;&nbsp;rescue Error =\> e  
> &nbsp;&nbsp;&nbsp;&nbsp;elt = (exhandlers.select{|obj| e.class == obj.exception\_class})[0]

or raise

> &nbsp;&nbsp;&nbsp;&nbsp;elt.exception\_handler.call(e)  
> &nbsp;&nbsp;end  
> end

Ha! Very nice indeed! Didn't think of that one. 🙂

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> > In article \<2urqndF2cbspnU1@uni-berlin.de\>, Robert Klemme wrote:  
> > \>\> On Wed, 3 Nov 2004 13:20:28 +0900, Yohanes Santoso \> \>\> \<ysantoso-rubytalk@dessyku.is-a-geek.org\> wrote:

---

<div class="post-metadata">

**Author:** ![Yohanes\_Santoso1](https://avatars.discourse-cdn.com/v4/letter/y/ccd318/32.png) [@Yohanes\_Santoso1](https://rubytalk.org/u/Yohanes_Santoso1)\
**Post date:** [4 November 2004 02:16 UTC](https://rubytalk.org/t/programmatically-and-dynamically-catching-exceptions/14562/7 "2004-11-04T02:16:18Z")

</div>

"Robert Klemme" \<bob.news@gmx.net\> writes:

> > class Try  
> > &nbsp;&nbsp;def execute  
> > &nbsp;&nbsp;&nbsp;&nbsp;user\_proc.call(\*user\_proc\_args)  
> > &nbsp;&nbsp;rescue Error =\> e  
> > &nbsp;&nbsp;&nbsp;&nbsp;elt = (exhandlers.select{|obj| e.class == obj.exception\_class})[0]
> 
> or raise
> 
> > &nbsp;&nbsp;&nbsp;&nbsp;elt.exception\_handler.call(e)  
> > &nbsp;&nbsp;end  
> > end

Nice! But I think I have left out an important factor in the original  
solution. It shouldn't be e.class == obj.exception\_class, but rather  
e.kind\_of?(obj.exception\_class).

YS.

---

<div class="post-metadata">

**Author:** ![Zach\_Dennis1](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@Zach\_Dennis1](https://rubytalk.org/u/Zach_Dennis1)\
**Post date:** [4 November 2004 02:26 UTC](https://rubytalk.org/t/programmatically-and-dynamically-catching-exceptions/14562/8 "2004-11-04T02:26:31Z")

</div>

I'm doing some automated ftp'ing....

begin  
&nbsp;&nbsp;&nbsp;&nbsp;#ftp code here  
rescue Net::FTPPermError =\> e  
&nbsp;&nbsp;&nbsp;&nbsp;#do nothing for example sake  
end

Even though I rescue the Net::FTPPermError, it still makes it to $stderr if there is an error. What am I missing here? I have tried to rescue all Exceptions, etc..and it still gets printed. Looking at the code for chdir we have:

def chdir(dirname)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if dirname == ".."  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;voidcmd("CDUP")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue FTPPermError  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if $![0, 3] != "500"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise FTPPermError, $!  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd = "CWD " + dirname  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;voidcmd(cmd)  
end

If I try to use chdir in my example above to a bogus directory it still gets printed to $stderr, even though my rescue clause picks it up. Shouldn't that be the end of it?

Thanks,

Zach

---

<div class="post-metadata">

**Author:** ![benny1](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@benny1](https://rubytalk.org/u/benny1)\
**Post date:** [4 November 2004 15:48 UTC](https://rubytalk.org/t/programmatically-and-dynamically-catching-exceptions/14562/9 "2004-11-04T15:48:44Z")

</div>

dear list,

maybe I am a bit dump, but the code was only a part of the solution, right?

so I tried to paint the rest of the picture and this was the only way I  
could figure out how the "core" may fit into the rest.  
(some classes and Try#initialize and Try#add\_exhandler were missing,  
exhandlers is now attribute etc.)

I'm just curious if this was the way you all thought about it or if I did  
miss an easier solution .

regards,  
benny

######## the code ###########  
# general error class  
class Error \< RuntimeError  
attr\_accessor :exception\_handler  
def initialize(code)  
&nbsp;&nbsp;code.call(self)  
end  
end

# some specific self defined error class  
class TestError \< Error  
attr\_accessor :msg  
def initialize(msg = nil , &code)  
&nbsp;&nbsp;super(code) if code  
&nbsp;&nbsp;@msg = msg  
end  
end

# the Try class  
class Try

&nbsp;&nbsp;def initialize(\*args, &code)  
&nbsp;&nbsp;&nbsp;@user\_proc = code  
&nbsp;&nbsp;&nbsp;@user\_proc\_args = \*args  
&nbsp;&nbsp;&nbsp;@exhandlers =   
&nbsp;&nbsp;end  
&nbsp;&nbsp;  
&nbsp;&nbsp;def add\_exhandler(name, &block)  
&nbsp;&nbsp;&nbsp;eval("@exhandlers \<\< #{name.to\_s}.new() do |obj| obj.exception\_handler =  
block; end")  
&nbsp;&nbsp;end  
&nbsp;&nbsp;  
def execute  
&nbsp;&nbsp;&nbsp;@user\_proc.call(@user\_proc\_args)  
rescue Error =\> e  
&nbsp;&nbsp;&nbsp;elt = (@exhandlers.select{|obj| e.kind\_of? obj.class })[0] or raise  
&nbsp;&nbsp;elt.exception\_handler.call(e)  
&nbsp;&nbsp;&nbsp;end  
end

> **···**
>
> ######################  
> # a test  
> tryblock = Try.new {  
> puts "doing something..."  
> raise TestError, "my favorite error message"  
> #raise "some other error"  
> }
> 
> tryblock.add\_exhandler(:TestError) { | ex |  
> puts "TestError raised!  
> \n\tbacktrace:#{ex.backtrace}\n\tmessage:'#{ex.msg}'"  
> }
> 
> tryblock.execute
> 
> Yohanes Santoso wrote:
> 
> > "Robert Klemme" \<bob.news@gmx.net\> writes:
> > 
> > > > class Try  
> > > > &nbsp;&nbsp;def execute  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;user\_proc.call(\*user\_proc\_args)  
> > > > &nbsp;&nbsp;rescue Error =\> e  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;elt = (exhandlers.select{|obj| e.class == obj.exception\_class})[0]
> > > 
> > > or raise
> > > 
> > > > &nbsp;&nbsp;&nbsp;&nbsp;elt.exception\_handler.call(e)  
> > > > &nbsp;&nbsp;end  
> > > > end
> > 
> > Nice! But I think I have left out an important factor in the original  
> > solution. It shouldn't be e.class == obj.exception\_class, but rather  
> > e.kind\_of?(obj.exception\_class).
> > 
> > YS.

---

<div class="post-metadata">

**Author:** ![benny1](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@benny1](https://rubytalk.org/u/benny1)\
**Post date:** [4 November 2004 15:53 UTC](https://rubytalk.org/t/programmatically-and-dynamically-catching-exceptions/14562/10 "2004-11-04T15:53:43Z")

</div>

benny wrote:

> maybe I am a bit dump

Indeed, I am: that was to be proven. I meant "dumb" of course.

benny

---

<div class="post-metadata">

**Author:** ![Zach\_Dennis1](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@Zach\_Dennis1](https://rubytalk.org/u/Zach_Dennis1)\
**Post date:** [5 November 2004 05:28 UTC](https://rubytalk.org/t/programmatically-and-dynamically-catching-exceptions/14562/11 "2004-11-05T05:28:06Z")

</div>

I haven't seen a reply to this thread, so I'll give it one more go.....

When I rescue an Exception shouldn't it be my responsiblity as the developer to handle it and print it to STDOUT or STDERR? Why does Ruby 1.8.2 still output something to STDERR!!? Is this just how it works?

Zach

----CODE-----  
class MyException \< Exception; end

def method  
&nbsp;&nbsp;&nbsp;&nbsp;raise MyException  
end

begin  
&nbsp;&nbsp;&nbsp;&nbsp;method  
rescue MyException  
&nbsp;&nbsp;&nbsp;&nbsp;#shouldn't this rescue the error?  
&nbsp;&nbsp;&nbsp;&nbsp;# --Why does it still get outputted to my screen?  
end

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [5 November 2004 05:38 UTC](https://rubytalk.org/t/programmatically-and-dynamically-catching-exceptions/14562/12 "2004-11-05T05:38:39Z")

</div>

Zach Dennis wrote:

> I haven't seen a reply to this thread, so I'll give it one more go.....
> 
> When I rescue an Exception shouldn't it be my responsiblity as the developer to handle it and print it to STDOUT or STDERR? Why does Ruby 1.8.2 still output something to STDERR!!? Is this just how it works?
> 
> Zach
> 
> ----CODE-----  
> class MyException \< Exception; end
> 
> def method  
> &nbsp;&nbsp;&nbsp;raise MyException  
> end
> 
> begin  
> &nbsp;&nbsp;&nbsp;method  
> rescue MyException  
> &nbsp;&nbsp;&nbsp;#shouldn't this rescue the error?  
> &nbsp;&nbsp;&nbsp;# --Why does it still get outputted to my screen?  
> end

Are you sure that code, exactly, prints something to stderr or stdout? I can't reproduce it here with 1.8.2 or 1.9 snapshots.

---

<div class="post-metadata">

**Author:** ![Zach\_Dennis1](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@Zach\_Dennis1](https://rubytalk.org/u/Zach_Dennis1)\
**Post date:** [5 November 2004 05:47 UTC](https://rubytalk.org/t/programmatically-and-dynamically-catching-exceptions/14562/13 "2004-11-05T05:47:04Z")

</div>

Joel VanderWerf wrote:

> Zach Dennis wrote:
> 
> > I haven't seen a reply to this thread, so I'll give it one more go.....
> > 
> > When I rescue an Exception shouldn't it be my responsiblity as the developer to handle it and print it to STDOUT or STDERR? Why does Ruby 1.8.2 still output something to STDERR!!? Is this just how it works?
> > 
> > Zach
> > 
> > ----CODE-----  
> > class MyException \< Exception; end
> > 
> > def method  
> > &nbsp;&nbsp;&nbsp;raise MyException  
> > end
> > 
> > begin  
> > &nbsp;&nbsp;&nbsp;method  
> > rescue MyException  
> > &nbsp;&nbsp;&nbsp;#shouldn't this rescue the error?  
> > &nbsp;&nbsp;&nbsp;# --Why does it still get outputted to my screen?  
> > end
> 
> Are you sure that code, exactly, prints something to stderr or stdout? I can't reproduce it here with 1.8.2 or 1.9 snapshots.

Joel you are a lifesaver. Thank you for responding....I stepped out of my IDE shell and test just via command line. And you are correct, it doesn't print to stdout or stderr. So then I looked at the difference between how my IDE was running Ruby and me personally on the CLI. The debug "-d" option made the difference when calling Ruby. Bah, I guess that is what happens when you get real comfortable in an IDE.

Thanks so much Joel for responding, otherwise I'd probably still be at this minor roadblock tomorrow!

Zach
