Please sign in to comment.
Add explanation + fixture for begin
- Loading branch information...
Showing with 25 additions and 0 deletions.
|@@ -0,0 +1,16 @@|
|+## `begin` statement|
|+A `begin` statement is primarily used when a piece of code may raise an exception that we wish to `rescue` from or `ensure` that even if an exception is raised that more code is ran. An example:|
|+ rescue Boom => e|
|+ puts "No more boom."|
|+ puts "Everything is OK."|
|+In this example, if the `boom!` method raises a `Boom` exception, this will be caught by the `rescue` statement which assigns the rescued exception to the local variable `e`. Everything after the `rescue` but before the following `end` or `ensure` is the code that will be ran during the `rescue`. To get the stacktrace or message of this exception we can call `stacktrace` and `message` respectively on this object.|
|+The `ensure` statement declares code that should be ran always, regardless of if an exception was rescued.|