Error Handling in Ruby: Part I

Mehdi Farsi
Aug 13, 2018 · 3 min read

What’s an error in Ruby ?

irb> RuntimeError.ancestors.include? Exception
=> true
irb> NoMethodError.ancestors.include? Exception
=> true

How to throw & catch an error ?

irb> raise 'an error occurred'
RuntimeError: an error occurred
irb> raise NoMethodError, 'an error occurred'
NoMethodError
: an error occurred
NoMethodError: an error occurred
ZeroDivisionError: divided by 0
NoMethodError: undefined method `odd?' for "my string":String

Errors & magic variables

NoMethodError: undefined method `odd?' for "my string":String
/workspace.rb:87:in `eval'
/workspace.rb:87:in `evaluate'
...
/bin/irb:11:in `<main>'

May I have your attention please 🎤🎤

Feel free to subscribe here: www.rubycademy.com


rubycademy

E-Learning platform for Ruby and Ruby on Rails

Mehdi Farsi

Written by

Founder of www.rubycademy.com — I blog about Ruby & ROR - Paris

rubycademy

E-Learning platform for Ruby and Ruby on Rails