Per method exception and ensure handlers
2006-04-17
In Ruby, methods automatically define begin/end blocks. That makes it easy to write exception handlers and ensure blocks:
1 def access!(options={}) 2 raise AuthorizationFailure if self.email != options[:email] 3 self.download_count += 1 4 5 rescue AuthorizationFailure 6 self.unauthorized_access_count += 1 7 raise 8 9 ensure 10 self.save! 11 end
It is important to re-raise the exception in the exception handler, or else the exception will be silently thrown away.
blog comments powered by Disqus