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?
on 2007-11-07 20:18
on 2007-11-07 20:25
On Nov 7, 12:40 pm, Casimir P <pikselNOSPA...@welNOSPMAMho.com> wrote: > me. :) > > What do you think? > > -- > Casimir Pohjanraito > Art Portfolio:http://csmr.dreamhosters.com 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 2007-11-07 21:17
On Nov 7, 2007, at 1:45 PM, Casimir P wrote: > 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
on 2007-11-07 22:59
Casimir P <pikselNOSPAMMi@welNOSPMAMho.com> writes: > me. :) > > What do you think? > > -- > Casimir Pohjanraito > Art Portfolio: http://csmr.dreamhosters.com 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 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
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.