[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Rambling: try/catch

Casimir P

11/7/2007 6:41:00 PM

Just had an vague idea about generic java "try-catch" converted to ruby-
world I would like some feedback on.

I'm building a "microscopic CMS" system to learn more ruby, and of course
it would be good to implement proper error catching system. Try-catch is
what java uses.

Now the vague idea was to create one error catching function, and to
throw all things that might fail (say loading a file) "through" that
function. Passed as a code block? I am not sure, it keeps slipping from
me. :)

What do you think?

--
Casimir Pohjanraito
Art Portfolio: http://csmr.dreamh...
3 Answers

yermej

11/7/2007 7:04:00 PM

0

On Nov 7, 12:40 pm, Casimir P <pikselNOSPA...@welNOSPMAMho.com> wrote:
> Just had an vague idea about generic java "try-catch" converted to ruby-
> world I would like some feedback on.
>
> I'm building a "microscopic CMS" system to learn more ruby, and of course
> it would be good to implement proper error catching system. Try-catch is
> what java uses.
>
> Now the vague idea was to create one error catching function, and to
> throw all things that might fail (say loading a file) "through" that
> function. Passed as a code block? I am not sure, it keeps slipping from
> me. :)
>
> What do you think?
>
> --
> Casimir Pohjanraito
> Art Portfolio:http://csmr.dreamh...

I'm not sure I completely understand (I've had a string of wrong
answers here recently), but I think you're looking for begin/rescue:

begin
do_something_that_might_explode()
rescue => e
puts "uh-oh, an exception: #{e}: #{e.backtrace.join("\n")}"
end

fedzor

11/7/2007 8:14:00 PM

0


On Nov 7, 2007, at 1:45 PM, Casimir P wrote:

> Just had an vague idea about generic java "try-catch" converted to
> ruby-
> world I would like some feedback on.
>
> I'm building a "microscopic CMS" system to learn more ruby, and of
> course
> it would be good to implement proper error catching system. Try-
> catch is
> what java uses.
>
> Now the vague idea was to create one error catching function, and to
> throw all things that might fail (say loading a file) "through" that
> function. Passed as a code block? I am not sure, it keeps slipping
> from
> me. :)


I **think** this is what you're looking for:

begin
# do something useful...
rescue => e
# do something with the error 'e'
end

To keep rescue code short, you could have a function:
error_function = lambda { | error |
#do something useful...
}

and then for rescue...

begin
....
rescue => e
error_function[e]
end

Tadah!

HTH,
ari


Yohanes Santoso

11/7/2007 9:58:00 PM

0

Casimir P <pikselNOSPAMMi@welNOSPMAMho.com> writes:

> Just had an vague idea about generic java "try-catch" converted to ruby-
> world I would like some feedback on.
>
> I'm building a "microscopic CMS" system to learn more ruby, and of course=
=20
> it would be good to implement proper error catching system. Try-catch is=
=20
> what java uses.
>
> Now the vague idea was to create one error catching function, and to=20
> throw all things that might fail (say loading a file) "through" that=20
> function. Passed as a code block? I am not sure, it keeps slipping from=20
> me. :)
>
> What do you think?
>
> --=20
> Casimir Pohjanraito=20
> Art Portfolio: http://csmr.dreamh...

The idea of passing around a signal handler is not new.=20
It allows you to handle low-level condition in a high-level manner.

Here is one, done =C3=A0 la SRFI-34.



def with_exception_handler(k =3D nil)=20
begin
yield
rescue Exception =3D> e
if k then if k.call(e) =3D=3D :retry then retry end end
end
end


limited_patience_handler =3D=20
lambda {
# can be called to handle exception three times.
# after that it loses patience and just reraise things.
i=3D[1,2,"last one"]
lambda{|e| if not i.empty?
puts "Got exception ##{i.shift}: #{e.inspect}"=20
:retry
else
puts "I'm done covering for you. Reraising to my superviso=
r"
raise e
end}}.call

with_exception_handler(limited_patience_handler) {
puts "Raising"
raise "Test exception"
puts "Never reach here"
}



****Output:

Raising
Got exception #1: #<RuntimeError: Test exception>
Raising
Got exception #2: #<RuntimeError: Test exception>
Raising
Got exception #last one: #<RuntimeError: Test exception>
Raising
I'm done covering for you. Reraising to my supervisor
/tmp/x.rb:24: Test exception (RuntimeError)
from /tmp/x.rb:3:in `with_exception_handler'
from /tmp/x.rb:22



YS