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'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
···
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.
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
···
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.
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
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.
The idea of passing around a signal handler is not new.
It allows you to handle low-level condition in a high-level manner.
Here is one, done à la SRFI-34.
def with_exception_handler(k = nil)
begin
yield
rescue Exception => e
if k then if k.call(e) == :retry then retry end end
end
end
limited_patience_handler =
lambda {
# can be called to handle exception three times.
# after that it loses patience and just reraise things.
i=[1,2,"last one"]
lambda{|e| if not i.empty?
puts "Got exception ##{i.shift}: #{e.inspect}"
:retry
else
puts "I'm done covering for you. Reraising to my supervisor"
raise e
end}}.call
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