Ruby Academy · Lesson

Handling Errors with begin and rescue

Prevent crashes by catching and handling errors with exception handling.

Lesson 1 of 310 steps

Handling Errors with begin and rescue is a free Ruby Academy lesson on CoddyKit. This is lesson 1 of 3. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 3 lessons in total.

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.

Handling Errors with begin and rescue — illustration 1

Using begin and rescue

Use begin and rescue to catch and handle errors.

begin
  puts 10 / 0
rescue ZeroDivisionError
  puts 'Cannot divide by zero!'
end

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!'
end

Accessing the Exception Object

Use rescue => e to access the exception object.

begin
  puts 10 / 0
rescue ZeroDivisionError => e
  puts "Error: #{e.message}"
end

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.'
end

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
end

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 }
end

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.'
end

Congratulations!

You have learned how to handle errors using begin and rescue.

Next, you will explore how to raise and customize exceptions.

Handling Errors with begin and rescue — illustration 10
Free to start

Learn Ruby with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
29
Lessons
109

Frequently Asked Questions

Is the “Handling Errors with begin and rescue” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “Handling Errors with begin and rescue”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 3 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.

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

  1. Handling Errors with begin and rescue
  2. Raising and Customizing Exceptions
  3. Debugging Techniques
← Back to Ruby Academy