A single exception uses the FAIL keyword only to catch an exception and throw it over and over again (because you are not failing here, but are accurate and intentionally throwing an exception).
Begin
fail ' Oops '
rescue => error
raise if Error.message!= ' Oops '
end
Do not specify the exact runtimeerror for fail/raise.
# bad
fail runtimeerror, "message"
# Good-signals a runtimeerror by default
fail ' message '
Rather than an exception instance, an exception class and a message are preferred as two arguments for fail/raise.
# bad
fail someexception.new ("message")
# Note which there is no way to does ' fail someexception.new (' message '), BA Cktrace '.
# good
fail someexception, ' message '
# consistent with ' fail someexception, ' message ', BackTrace '.
Do not return in the ensure block. If you explicitly return from a method in a ensure block, the return will be superior to any thrown exception, and will return even if no exception is thrown. In fact, the anomaly will slip away quietly.
def foo
begin
fail
ensure return
"very
bad idea" end
Use implicit the begin blocks when possible. If possible, an implicit begin code block can be used.
# bad
def foo
begin
# main logic goes
Rescue
# failure handling goes
# good
def foo
# main logic goes here
Rescue
# failure handling goes
By contingency methods Contingency method. (a word created by AVDI Grimm) to reduce the use of begin blocks.
# bad
begin
something_that_might_fail
rescue ioerror
# handle ioerror
end
begin
Something_else_that_might_fail
Rescue IOError
# handle IOError
End
# good
def with_io_error_ Handling
Yield
rescue IOError
# handle ioerror end with_io_error_handling
{something_that _might_fail}
with_io_error_handling {something_else_that_might_fail}
Do not suppress exception output.
# bad
begin
# A exception occurs here
rescue Someerror
# The rescue clause does absolutely 5/>end
# Bad
do_something rescue Nil
Avoid using the rescue modifier form.
# Bad-this catches exceptions of StandardError class and its descendant classes Read_file rescue
(Handle_error)
# Good-this catches the exceptions of Errno::enoent class and its descendant classes
def foo
read_file
Rescue Errno::enoent => ex
Handle_error (ex)
end
Do not use exceptions to control the flow.
# bad
begin
n/d
rescue Zerodivisionerror
puts ' cannot divide by 0! '
End
# good
if D.zero?
Puts "cannot divide by 0!"
else
n/d
End
You should always avoid blocking (top-level) Exception exception classes. Here (Ruby itself) will capture the signal and call exit, requiring you to use kill-9 to kill the process.
# bad
begin
# calls to exit and kill signals'll be caught (except kill-9)
exit
Rescue
put S "You didn ' t really want to exit, right?"
# exception Handling
End
# good
begin
# A blind rescue rescues to StandardError, not exception as many< c11/># programmers assume.
Rescue => E
# exception handling
End
# also good
begin
# A exception occurs
here Rescue StandardError => E
# exception handling
End
Put more specific exceptions above the rescue (rescue) chain, otherwise they will not be rescued.
# bad
begin
# some code
rescue Exception => E
# Some handling
rescue StandardError => 8/># Some handling
end
# good
begin
# some code
rescue StandardError => e
# some Handling
Rescue Exception => E
# some handling
end
Release the external resources that your program obtains in the ensure block.
f = File.Open (' testfile ')
begin
# ... process
Rescue
#. Handle error
ensure
f.close unless F.nil?
End
Unless necessary, use the exception class in the Ruby standard library as much as possible, rather than introducing a new exception class. (instead of deriving your own exception class)