Exceptions

Contents
Raise
Rescue
Ensure always runs
Your own error class

Raise

The keyword raise stops the program with an error object. A plain string becomes a RuntimeError carrying that message. Naming a class with a message pinpoints the failure more precisely.

raise "boom" raise ArgumentError, "bad input"

Left alone, either line ends the program and prints the message with a backtrace.

Rescue

The keywords begin and rescue wrap risky code: the rescue branch runs when the body raises. Naming an error class catches only that family, while other errors keep traveling upward. Match the class to the failures you truly expect.

begin raise "boom" rescue RuntimeError puts "caught" end

caught

Ensure always runs

The keyword ensure closes a begin block with cleanup that always runs: after success, after a rescued failure, and even when an unexpected error escapes. Closing files and releasing locks belongs there.

begin raise "boom" rescue RuntimeError puts "caught" ensure puts "cleanup" end

caught
cleanup

Your own error class

Subclassing StandardError defines an error type of your own, carrying the intent of the domain. Rescuing it by name keeps application failures apart from ordinary bugs. Give the class a clear name so future readers grasp the rescue at a glance.

class AppError < StandardError end begin raise AppError, "nope" rescue AppError puts "app failed" end

app failed

Next: Modules and mixins

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: