Syntax
begin
risky_work
rescue StandardError => error
puts error.message
endExamples
Handling a conversion error
A small, runnable example of this syntax.
begin
age = Integer("not a number")
rescue ArgumentError => error
puts "Please enter a number: #{error.message}"
endBest practices
- Rescue specific exception classes when possible so unexpected failures are not silently hidden.
- Keep examples small while learning, then combine the idea with a real project.
Runtime & versions
Ruby interpreter. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Ruby documentation