Skip to content

Commit

Permalink
Document use of ensure and else at method level [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyevans committed Jul 19, 2019
1 parent ceeb153 commit c945d11
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions doc/syntax/methods.rdoc
Expand Up @@ -475,6 +475,28 @@ May be written as:
# handle exception
end

Similarly, if you wish to always run code even if an exception is raised,
you can use +ensure+ without +begin+ and +end+:

def my_method
# code that may raise an exception
ensure
# code that runs even if previous code raised an exception
end

You can also combine +rescue+ with +ensure+ and/or +else+, without
+begin+ and +end+:

def my_method
# code that may raise an exception
rescue
# handle exception
else
# only run if no exception raised above
ensure
# code that runs even if previous code raised an exception
end

If you wish to rescue an exception for only part of your method, use +begin+ and
+end+. For more details see the page on {exception
handling}[rdoc-ref:syntax/exceptions.rdoc].

0 comments on commit c945d11

Please sign in to comment.