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.

Search

Your Host

A picture of me

I am François Beausoleil, a Ruby on Rails and Scala developer. During the day, I work on Seevibes, a platform to measure social interactions related to TV shows. At night, I am interested many things. Read my biography.

Top Tags

Books I read and recommend

Links

Projects I work on

Projects I worked on