Permalink
Browse files

Chapter Exceptions

  • Loading branch information...
1 parent 83be8ce commit 23ac71f8f8e46f34872b87da0b6c1fb23fd76185 @weppos committed Feb 23, 2011
Showing with 116 additions and 0 deletions.
  1. +113 −0 exceptions/01_exceptions.md
  2. +3 −0 styles.css
View
@@ -1,2 +1,115 @@
+!SLIDE subsection
+# Exceptions
+
+
!SLIDE
# Exceptions
+
+ @@@ Ruby
+ begin
+ # execute
+ rescue
+ # handle error
+ else
+ # execute when no errors
+ ensure
+ # always execute, with/without errors
+ end
+
+
+!SLIDE
+# `begin` / `rescue`
+
+ @@@ Ruby
+ begin
+ raise CustomError, "This is the message"
+ rescue CustomError => error
+ p error.message
+ end
+
+ # => "This is the message"
+
+
+!SLIDE
+# Defaults
+
+ @@@ Ruby
+ begin
+ # When you don't specify an Error class,
+ # by default raises a RuntimeError
+ raise "Boom!"
+
+ # Rescues all kind of StandardError
+ rescue => error
+
+ # ...
+
+ end
+
+ # => "Boom!"
+
+
+!SLIDE
+# Raising exceptions
+
+ @@@ Ruby
+ # Raises a RuntimeError with empty message
+ raise
+
+ # Raise a RuntimeError with message
+ raise "Boom!"
+
+ # Raise a CustomError with message
+ raise CustomError, "Boom!"
+
+
+!SLIDE
+# Rescuing exceptions
+
+ @@@ Ruby
+ begin
+ raise "This is the message"
+
+ # rescues all kind of FooError
+ rescue FooError => error
+ # ...
+
+ # rescues all kind of BarError
+ rescue BarError => error
+ # ...
+
+ end
+
+!SLIDE
+# Implicit `begin`
+
+ @@@ Ruby
+ def hello
+ ...
+ rescue CustomError => error
+ ...
+ rescue => error
+ ...
+ rescue
+ ...
+ end
+
+
+!SLIDE
+# `retry`
+
+ @@@ Ruby
+ retries = 0
+
+ begin
+ Email.deliver
+ rescue Email::Error => error
+ if retries < 3
+ retries += 1
+ sleep 1
+ retry
+ else
+ raise
+ end
+ end
+
View
@@ -0,0 +1,3 @@
+pre {
+ font-size: 2em;
+}

0 comments on commit 23ac71f

Please sign in to comment.