SyntaxDoes "raise" trigger exception?RescueRescue all exceptionsRescue from StandardErrorRaiseRaise a single stringRelated Articles
Syntax
begin
# raise 'A test exception.'
puts "I'm not raising exception"
rescue Exception => e
puts e.message
puts e.backtrace.inspect
else
puts "Congratulations-- no errors!"
ensure
puts "Ensuring execution"
end
# I'm not raising exception
# Congratulations-- no errors!
# Ensuring executionDoes "raise" trigger exception?
Short answer: yes
Long answer: what exception are you raising? what exception are you capturing?
Rescue
Rescue all exceptions
rescue ExceptionRescue everything is a bad idea because it includes
SyntaxError, LoadError, and Interrupt.- Rescuing
Interruptprevents the user from using CTRLC to exit the program.
- Rescuing
SignalExceptionprevents the program from responding correctly to signals. It will be unkillable except by kill -9.
Rescue from StandardError
rescue without a parameter just rescues exceptions that inherit from StandardErrorThese three all rescue from
StandardError.begin
# iceberg!
rescue
# lifeboats
endbegin
# iceberg!
rescue => e
# lifeboats
endbegin
# iceberg!
rescue StandardError => e
# lifeboats
endRaise
raise exception-type "exception message" conditionRaise a single string
With a single String argument, raises a RuntimeError with the string as a message.
(RuntimeError inherit from StandardError)
raise "this is an error"Related Articles
Â