Handling Errors with begin and rescue
Prevent crashes by catching and handling errors with exception handling.
Handling Errors with begin and rescue is a free Ruby Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Handling Errors with begin and rescue
Errors are inevitable in programming, but Ruby provides mechanisms to handle them gracefully.
In this lesson, you will learn how to use begin and rescue to catch and handle exceptions.

2
Using begin and rescue
Use begin and rescue to catch and handle errors.
begin
puts 10 / 0
rescue ZeroDivisionError
puts 'Cannot divide by zero!'
end3
Handling Multiple Error Types
You can catch different error types using multiple rescue blocks.
begin
num = Integer('hello')
rescue ZeroDivisionError
puts 'Cannot divide by zero!'
rescue ArgumentError
puts 'Invalid number format!'
end4
Accessing the Exception Object
Use rescue => e to access the exception object.
begin
puts 10 / 0
rescue ZeroDivisionError => e
puts "Error: #{e.message}"
end5
Using ensure
The ensure block always runs, regardless of whether an exception occurred.
begin
puts 10 / 0
rescue ZeroDivisionError
puts 'Cannot divide by zero!'
ensure
puts 'This code runs no matter what.'
end6
Using retry
The retry keyword restarts the begin block from the beginning.
attempt = 0
begin
attempt += 1
puts 10 / 0
rescue ZeroDivisionError
puts 'Attempting again...'
retry if attempt < 2
end7
Logging Errors
Log errors to a file instead of displaying them to the user.
begin
puts 10 / 0
rescue ZeroDivisionError => e
File.open('error.log', 'a') { |f| f.puts e.message }
end8
9
Displaying Custom Error Messages
Provide user-friendly error messages instead of raw exceptions.
begin
num = Integer('hello')
rescue ArgumentError
puts 'Oops! That was not a valid number.'
end10
Congratulations!
You have learned how to handle errors using begin and rescue.
Next, you will explore how to raise and customize exceptions.

Frequently asked questions
Is the “Handling Errors with begin and rescue” lesson free?
Yes — the full text of “Handling Errors with begin and rescue” is free to read here on the web, and the Ruby Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
What will I learn in “Handling Errors with begin and rescue”?
Prevent crashes by catching and handling errors with exception handling. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Handling Errors with begin and rescue” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Handling Errors with begin and rescue
- Raising and Customizing Exceptions
- Debugging Techniques