Rescue clause or equivalent for catch?

Does having a rescue clause for catch sound like a good idea to anyone?

You could write this:

catch (:error) {

# Do stuff

do_stuff()		# throws :error
do_more_stuff()		# throws :error
finish_stuff()		# throws :error

rescue

# Oops, stuff failed, clean it

cleanup_stuff()

end
do_junk_whether_stuff_failed_or_not()

instead of this

catch(:done) { 
catch (:error) { 

    # Do stuff

    do_stuff()		# throws :error
    do_more_stuff()	# throws :error
    finish_stuff()      # throws :error
    throw :done
}
# Oops, error, clean up
cleanup_stuff()
}
do_junk_whether_stuff_failed_or_not()

Just wondering.

···

Jim Hranicky, Senior SysAdmin UF/CISE Department |
E314D CSE Building Phone (352) 392-1499 |
jfh@cise.ufl.edu http://www.cise.ufl.edu/~jfh |


“Given a choice between a complex, difficult-to-understand, disconcerting
explanation and a simplistic, comforting one, many prefer simplistic
comfort if it’s remotely plausible, especially if it involves blaming
someone else for their problems.”
– Bob Lewis, Infoworld

    catch (:error) {
  # Do stuff
  do_stuff() # throws :error
  do_more_stuff() # throws :error
  finish_stuff() # throws :error
    rescue
  # Oops, stuff failed, clean it
  cleanup_stuff()
    end

You can write

   if catch (:error) do
         do_stuff() # throws :error, true
         do_more_stuff() # throws :error, true
         finish_stuff() # throws :error, true
         nil
      end
      cleanup_stuff()
   end

Important to call 'throws :error, true'

Guy Decoux

Just wrap the code you want rescued in begin … rescue … end.

Gavin

···

----- Original Message -----
From: “James F.Hranicky” jfh@cise.ufl.edu
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, September 18, 2002 11:51 PM
Subject: Rescue clause or equivalent for catch?