Forum: Ruby Rambling: try/catch

900474ad42ce6cf8d55ee7136a55255c?d=identicon&s=25 Casimir P (Guest)
on 2007-11-07 20:18
(Received via mailing list)
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?
Bc8e3e2f7acb3a7d93c10e235dc0b4e1?d=identicon&s=25 yermej (Guest)
on 2007-11-07 20:25
(Received via mailing list)
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
Dec001d46d9ccb152be2f2c8a143934d?d=identicon&s=25 thefed (Guest)
on 2007-11-07 21:17
(Received via mailing list)
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
B000982a23d5c6a34292902caf225dd7?d=identicon&s=25 Yohanes Santoso (Guest)
on 2007-11-07 22:59
(Received via mailing list)
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

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.