Rambling: try/catch

C

Casimir P

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?
 
Y

yermej

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?

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
 
T

thefed

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
 
Y

Yohanes Santoso

Casimir P said:
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.dreamhosters.com

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
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top