ruby on rails - Abstracting exception checking -


i have range of methods use same exception handling.

how can abstract out exception checking separate function?

see example below, lot folks!

def   code   begin     rescue 1...     rescue 2...     rescue 3...     rescue 4...   end end  def b   code   begin     rescue 1...     rescue 2...     rescue 3...     rescue 4...   end end 

the simplest solution pass code method block , yield within begin/rescue expression:

def run_code_and_handle_exceptions   begin     yield   rescue 1...   rescue 2...   rescue 3...   rescue 4...   end end  # elsewhere... def   run_code_and_handle_exceptions     code   end end # etc... 

you may want come more succinct method name run_code_and_handle_exceptions!


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -