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.
At a glance
- Purpose
- Expressive scripting and applications
- File extension
- .rb
- Runs in
- Ruby interpreter
- Usually used with
- Ruby standard library and gems
Specifications & further reading
Related Ruby documentation
Ruby Variables & Strings
Ruby uses local variables without a declaration keyword. Strings can be enclosed in single or double quotes; double-quoted strings support interpolation with #{...}.Ruby comments
Ruby reference for ruby comments.Ruby local variables
Ruby reference for ruby local variables.Ruby numbers
Ruby reference for ruby numbers.
Ruby uses local variables without a declaration keyword. Strings can be enclosed in single or double quotes; double-quoted strings support interpolation with #{...}.Ruby comments
Ruby reference for ruby comments.Ruby local variables
Ruby reference for ruby local variables.Ruby numbers
Ruby reference for ruby numbers.