Manual update: custom exceptions (#12847) [backport]

Said that you can have custom exceptions and showed how to create and raise them.

(cherry picked from commit 73dd348ddd)
This commit is contained in:
Mark
2019-12-08 21:39:44 +00:00
committed by narimiran
parent 00408a4554
commit 512727d75e

View File

@@ -4126,6 +4126,23 @@ error message from ``e``, and for such situations it is enough to use
except:
echo getCurrentExceptionMsg()
Custom exceptions
-----------------
Is it possible to create custom exceptions. These make it easy to distinguish between exceptions raised by nim and those from your own code.
A custom exception is a custom type:
.. code-block:: nim
type
LoadError* = object of Exception
Ending the custom exception's name with ``Error`` is recommended.
Custom exceptions can be raised like any others, e.g.:
.. code-block:: nim
raise newException(LoadError, "Failed to load data")
Defer statement
---------------