Syntax
begin
Integer("oops")
rescue ArgumentError => error
puts error.message
endExamples
Ruby begin and rescue example
A focused example you can adapt while practising.
begin
Integer("oops")
rescue ArgumentError => error
puts error.message
endBest practices
- Start with the smallest working example, then adapt it to your project.
- Use the language’s standard library and idioms before introducing extra dependencies.
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 Exceptions
Ruby signals exceptional failures by raising exceptions. rescue handles a matching exception, while ensure runs whether the operation succeeded or failed.Ruby comments
Ruby reference for ruby comments.Ruby local variables
Ruby reference for ruby local variables.
Ruby uses local variables without a declaration keyword. Strings can be enclosed in single or double quotes; double-quoted strings support interpolation with #{...}.Ruby Exceptions
Ruby signals exceptional failures by raising exceptions. rescue handles a matching exception, while ensure runs whether the operation succeeded or failed.Ruby comments
Ruby reference for ruby comments.Ruby local variables
Ruby reference for ruby local variables.