diff options
author | Jeremy Evans <code@jeremyevans.net> | 2019-07-19 11:36:12 -0700 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2019-07-19 11:39:05 -0700 |
commit | c945d115a55710089ac23027c74ed32a40cd9e50 (patch) | |
tree | e833f50e773551075c1429dde0eb8b9d61f3bd72 /doc/syntax | |
parent | ceeb1535dd6f618ac6069415a69be6d8b2916450 (diff) |
Document use of ensure and else at method level [ci skip]
Diffstat (limited to 'doc/syntax')
-rw-r--r-- | doc/syntax/methods.rdoc | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/doc/syntax/methods.rdoc b/doc/syntax/methods.rdoc index 94fed45e9a..6424c9d9ec 100644 --- a/doc/syntax/methods.rdoc +++ b/doc/syntax/methods.rdoc @@ -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]. |