diff options
author | Maurizio Tomasi <ziotom78@gmail.com> | 2015-01-14 14:20:21 +0100 |
---|---|---|
committer | Maurizio Tomasi <ziotom78@gmail.com> | 2015-01-14 14:20:21 +0100 |
commit | 0f65c23a0be43085501e9b834f96e0c9035e67ee (patch) | |
tree | e7bdefed4021d96b2ebe736fffb0c0cf27e6c8ff | |
parent | 33c587d06b0c24e571ac7f9b8ae3e565df1ab9f5 (diff) | |
download | Nim-0f65c23a0be43085501e9b834f96e0c9035e67ee.tar.gz |
New section "Except clauses" in the manual
This new section explains how to use `getCurrentException` and `getCurrentExceptionMsg`. See the thread http://forum.nim-lang.org/t/752 .
-rw-r--r-- | doc/manual/exceptions.txt | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/doc/manual/exceptions.txt b/doc/manual/exceptions.txt index e40742e0a..f18ab5635 100644 --- a/doc/manual/exceptions.txt +++ b/doc/manual/exceptions.txt @@ -67,6 +67,41 @@ follows a ``(`` it has to be written as a one liner: let x = (try: parseInt("133a") except: -1) +Except clauses +-------------- + +Within an ``except`` clause, it is possible to use +``getCurrentException`` to retrieve the exception that has been +raised: + +.. code-block:: nim + try: + # ... + except IOError: + let e = getCurrentException() + # Now use "e" + +Note that ``getCurrentException`` always returns a ``ref Exception`` +type. If a variable of the proper type is needed (in the example +above, ``IOError``), one must use an explicit cast: + +.. code-block:: nim + try: + # ... + except IOError: + let e = (ref IOError)(getCurrentException()) + # "e" is now of the proper type + +However, this is seldom needed. The most common case is to extract an +error message from ``e``, and for such situations it is enough to use +``getCurrentExceptionMsg``: + +.. code-block:: nim + try: + # ... + except IOError: + echo "I/O error: " & getCurrentExceptionMsg() + Defer statement --------------- |