Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Programmatically and dynamically catching exceptions

Reply
Thread Tools

Programmatically and dynamically catching exceptions

 
 
Logan Capaldo
Guest
Posts: n/a
 
      11-03-2004
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

begin
user_proc.call
[SomeEx1, SomeEx2, etc..].each do |ex|
rescue ex => e
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?


 
Reply With Quote
 
 
 
 
Yohanes Santoso
Guest
Posts: n/a
 
      11-03-2004
Logan Capaldo <(E-Mail Removed)> 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


YS.


 
Reply With Quote
 
 
 
 
Logan Capaldo
Guest
Posts: n/a
 
      11-03-2004
Thanks thats quite neat.

On Wed, 3 Nov 2004 13:20:28 +0900, Yohanes Santoso
<(E-Mail Removed)-a-geek.org> wrote:
> Logan Capaldo <(E-Mail Removed)> 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
>
>
> YS.
>
>



 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-03-2004

"Logan Capaldo" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> Thanks thats quite neat.
>
> On Wed, 3 Nov 2004 13:20:28 +0900, Yohanes Santoso
> <(E-Mail Removed)-a-geek.org> wrote:
> > Logan Capaldo <(E-Mail Removed)> 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

Kind regards

robert

 
Reply With Quote
 
Tim Sutherland
Guest
Posts: n/a
 
      11-03-2004
In article <(E-Mail Removed)>, Robert Klemme wrote:
>
>"Logan Capaldo" <(E-Mail Removed)> schrieb im Newsbeitrag
>news:(E-Mail Removed). ..
>> Thanks thats quite neat.
>>
>> On Wed, 3 Nov 2004 13:20:28 +0900, Yohanes Santoso
>> <(E-Mail Removed)-a-geek.org> wrote:
>> > Logan Capaldo <(E-Mail Removed)> 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
def execute
user_proc.call(*user_proc_args)
rescue Error => e
elt = (exhandlers.select{|obj| e.class == obj.exception_class})[0] or raise
elt.exception_handler.call(e)
end
end
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-03-2004

"Tim Sutherland" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> In article <(E-Mail Removed)>, Robert Klemme wrote:
> >
> >"Logan Capaldo" <(E-Mail Removed)> schrieb im Newsbeitrag
> >news:(E-Mail Removed). ..
> >> Thanks thats quite neat.
> >>
> >> On Wed, 3 Nov 2004 13:20:28 +0900, Yohanes Santoso
> >> <(E-Mail Removed)-a-geek.org> wrote:
> >> > Logan Capaldo <(E-Mail Removed)> 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
> def execute
> user_proc.call(*user_proc_args)
> rescue Error => e
> elt = (exhandlers.select{|obj| e.class == obj.exception_class})[0]

or raise
> elt.exception_handler.call(e)
> end
> end


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

robert

 
Reply With Quote
 
Yohanes Santoso
Guest
Posts: n/a
 
      11-04-2004
"Robert Klemme" <(E-Mail Removed)> writes:

>> class Try
>> def execute
>> user_proc.call(*user_proc_args)
>> rescue Error => e
>> elt = (exhandlers.select{|obj| e.class == obj.exception_class})[0]

> or raise
>> elt.exception_handler.call(e)
>> 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.


 
Reply With Quote
 
Zach Dennis
Guest
Posts: n/a
 
      11-04-2004
I'm doing some automated ftp'ing....

begin
#ftp code here
rescue Net::FTPPermError => e
#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)
if dirname == ".."
begin
voidcmd("CDUP")
return
rescue FTPPermError
if $![0, 3] != "500"
raise FTPPermError, $!
end
end
end
cmd = "CWD " + dirname
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


 
Reply With Quote
 
benny
Guest
Posts: n/a
 
      11-04-2004
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)
code.call(self)
end
end

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

# the Try class
class Try

def initialize(*args, &code)
@user_proc = code
@user_proc_args = *args
@exhandlers = []
end

def add_exhandler(name, &block)
eval("@exhandlers << #{name.to_s}.new() do |obj| obj.exception_handler =
block; end")
end

def execute
@user_proc.call(@user_proc_args)
rescue Error => e
elt = (@exhandlers.select{|obj| e.kind_of? obj.class })[0] or raise
elt.exception_handler.call(e)
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" <(E-Mail Removed)> writes:
>
>>> class Try
>>> def execute
>>> user_proc.call(*user_proc_args)
>>> rescue Error => e
>>> elt = (exhandlers.select{|obj| e.class == obj.exception_class})[0]

>> or raise
>>> elt.exception_handler.call(e)
>>> 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.


 
Reply With Quote
 
benny
Guest
Posts: n/a
 
      11-04-2004
benny wrote:

> maybe I am a bit dump

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

benny
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
defining, raising and catching exceptions Chris Hare Python 0 08-05-2010 10:41 PM
worker thread catching exceptions and putting them in queue Paul Sijben Python 5 03-08-2007 08:36 PM
Catching unhandled exceptions using HttpModules Amil Hanish ASP .Net 0 04-12-2006 11:51 PM
Try blocks and not catching exceptions Adam H. Peterson C++ 8 01-27-2004 11:34 PM
Re: catching exceptions from web user controls Marina ASP .Net 2 07-08-2003 04:48 PM



Advertisments