Ruby exceptions and rescue blocks
A common syntax pattern in Ruby is catching exceptions like this:
Inside the rescue
block you’ll have the exception object available in the predefined global variable $!
. An important thing to know is that this way you’ll catch only StandardError
(and derived) exceptions. Most ruby exceptions actually derive from this class, but some do not. If you want a true catch-all exception block you have to do like this:
Try it yourself:
This will show a RunTimeError
, caught by the simple rescue
statement.
This will give an uncaught SyntaxError
This will catch it.