class-wide begin/end blocks/exception handling

D

David E.

So I have a class with a lot of methods, and each method has a begin/end
block

class HiThere
def one(stext)
begin
puts("Hi");
rescue Exception => msg
self.exceptionHandle(msg)
end
end

def two(stext)
begin
puts("Hi");
rescue Exception => msg
self.exceptionHandle(msg)
end
end

#etc etc
#...
end

... Is there a way to put one begin/end block encompassing the whole
class/object? Sort of like an instance-wide begin/end block?

hehe<--- but serious...
 
M

Michael Granger

[...]
... Is there a way to put one begin/end block encompassing the whole
class/object? Sort of like an instance-wide begin/end block?

There isn't any syntax that lets you rescue any exception for the whole
class, but you can add a rescue clause to an entire method, saving you a
'begin' and an 'end'. E.g., instead of:

def one(stext)
begin
puts("Hi");
rescue Exception => msg
self.exceptionHandle(msg)
end
end

you'd have:

def one(stext)
puts("Hi");
rescue Exception => msg
self.exceptionHandle(msg)
end

This is (IMO) far more readable than the alternative.

You could probably add exception-handling for any method on a whole
class using clever aliasing and the 'method_added' hook or a
declarative, but down that path lies madness in the face of ScriptErrors
or any exception you haven't anticipated.
 
R

Robert Klemme

So I have a class with a lot of methods, and each method has a begin/end
block

class HiThere
=A0def one(stext)
=A0begin
=A0 puts("Hi");
=A0rescue Exception =3D> msg
=A0 self.exceptionHandle(msg)
=A0end
=A0end

=A0def two(stext)
=A0begin
=A0 puts("Hi");
=A0rescue Exception =3D> msg
=A0 self.exceptionHandle(msg)
=A0end
=A0end

=A0#etc etc
=A0#...
end

... Is there a way to put one begin/end block encompassing the whole
class/object? Sort of like an instance-wide begin/end block?

Not directly, but you can do

# needs 1.9
class Proxy < BasicObject
def initialize(obj)
@obj =3D obj
end

def method_missing(*a,&b)
@obj.send(*a,&b)
rescue ::Exception =3D> e
handle_exception e
end

def handle_exception(e)
puts e.message
end
end

irb(main):016:0> s =3D "foo"
=3D> "foo"
irb(main):017:0> s + "bar"
=3D> "foobar"
irb(main):018:0> s + 99
TypeError: can't convert Fixnum into String
from (irb):18:in `+'
from (irb):18
from /opt/bin/irb19:12:in `<main>'
irb(main):019:0> pr =3D Proxy.new(s)
=3D> "foo"
irb(main):020:0> pr + "bar"
=3D> "foobar"
irb(main):021:0> pr + 99
can't convert Fixnum into String
=3D> nil
irb(main):022:0>

HTH

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
R

Robert Klemme

Not directly, but you can do

Even better

class Proxy < BasicObject
def initialize(obj, &handler)
@obj =3D obj
@handler =3D handler
end

def method_missing(*a,&b)
@obj.send(*a,&b)
rescue ::Exception =3D> e
@handler and @handler[e]
end
end

def Proxy(obj, &handler)
Proxy.new(obj, &handler)
end

irb(main):016:0> s =3D "foo"
=3D> "foo"
irb(main):017:0> s + "bar"
=3D> "foobar"
irb(main):018:0> s + 99
TypeError: can't convert Fixnum into String
from (irb):18:in `+'
from (irb):18
from /opt/bin/irb19:12:in `<main>'
irb(main):019:0> pr =3D Proxy(s) {|e| p e.message}
=3D> "foo"
irb(main):020:0> pr + "bar"
=3D> "foobar"
irb(main):021:0> pr + 99
"can't convert Fixnum into String"
=3D> "can't convert Fixnum into String"
irb(main):022:0>

Cheers

robert



--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

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

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top