Handling Errors with Multiple Rescue Clauses in Ruby
📖 Scenario: Imagine you are writing a small program that divides numbers. Sometimes, the user might enter zero or a wrong input, causing errors. You want to handle these errors nicely so the program doesn't crash.
🎯 Goal: You will build a Ruby program that divides two numbers and uses multiple rescue clauses to handle different types of errors separately.
📋 What You'll Learn
Create two variables with exact values for numerator and denominator
Create a variable called
result initialized to nilUse a
begin block to perform divisionAdd multiple
rescue clauses to handle ZeroDivisionError and TypeErrorPrint the
result or an error message💡 Why This Matters
🌍 Real World
Handling errors gracefully is important in real programs to avoid crashes and give helpful messages to users.
💼 Career
Knowing how to use multiple rescue clauses helps you write robust Ruby code that can handle different problems clearly.
Progress0 / 4 steps