diff options
author | Araq <rumpf_a@web.de> | 2014-12-24 02:59:54 +0100 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2014-12-24 02:59:54 +0100 |
commit | a505918e0ce860e8289844a1a5da8f913998110e (patch) | |
tree | 3da00d8de10e16f87d0aae8482f1c05a1a37c931 | |
parent | 59e279ba9cd0cedb8021c9f8cd425cb38e128224 (diff) | |
download | Nim-a505918e0ce860e8289844a1a5da8f913998110e.tar.gz |
documented 'defer' statement
-rw-r--r-- | doc/manual/exceptions.txt | 41 |
1 files changed, 18 insertions, 23 deletions
diff --git a/doc/manual/exceptions.txt b/doc/manual/exceptions.txt index 7cad1c2b4..311a810ff 100644 --- a/doc/manual/exceptions.txt +++ b/doc/manual/exceptions.txt @@ -47,36 +47,31 @@ the rest of the procedure - that is not within a ``finally`` clause - is not executed (if an exception occurs). -Standalone except and finally statements ----------------------------------------- +Defer statement +--------------- + +Instead of a ``try finally`` statement a ``defer`` statement can be used. -``except`` and ``finally`` can also be used as a stand-alone statements. -Any statements following them in the current block will be considered to be -in an implicit try block: +Any statements following the ``defer`` in the current block will be considered +to be in an implicit try block: .. code-block:: nim var f = open("numbers.txt") - finally: close(f) - ... + defer: close(f) + f.write "abc" + f.write "def" -The ``except`` statement has a limitation in this form: one can't specify the -type of the exception, one has to catch everything. Also, if one wants to use -both ``finally`` and ``except`` one needs to reverse the usual sequence of the -statements. Example: +Is rewritten to: .. code-block:: nim - proc test() = - raise newException(Exception, "Hey ho") - - proc tester() = - finally: echo "3. Finally block" - except: echo "2. Except block" - echo "1. Pre exception" - test() - echo "4. Post exception" - # --> 1, 2, 3 is printed, 4 is never reached - -Top level standalone ``finally`` or ``except`` statements are not supported + var f = open("numbers.txt") + try: + f.write "abc" + f.write "def" + finally: + close(f) + +Top level ``defer`` statements are not supported since it's unclear what such a statement should refer to. |