Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Chapter Exceptions

  • Loading branch information...
commit 23ac71f8f8e46f34872b87da0b6c1fb23fd76185 1 parent 83be8ce
@weppos authored
Showing with 116 additions and 0 deletions.
  1. +113 −0 exceptions/01_exceptions.md
  2. +3 −0  styles.css
View
113 exceptions/01_exceptions.md
@@ -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
3  styles.css
@@ -0,0 +1,3 @@
+pre {
+ font-size: 2em;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.