1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
.. _WARNING:
Warnings
########
.. grid:: 2
.. grid-item::
:columns: 6
A warning can be raised similar to an exception, but it doesn't interrupt execution at the position where it was
raised. The warning travels upwards the call-stack until it's handled by a :class:`~pyTooling.Warning.WarningCollector`
similar to a `try .. except` statement. If a warning isn't handled within the call-stack, it raises an exception.
A warning is raised by Calling the class-method :meth:`WarningCollector.Raise <pyTooling.Warning.WarningCollector.Raise>`.
This function expects a single parameter: an instance of :class:`Warning`.
To handle a raised warning, a `with`-statement is used to collect raised warnings. Usually, a list is handed over
to a :class:`~pyTooling.Warning.WarningCollector` context.
.. grid-item::
:columns: 6
.. code-block:: Python
from pyTooling.Warning import WarningCollector
class ClassA:
def methA(self) -> None:
WarningCollector.Raise(Warning("Warning from ClassA.methA"))
.. code-block:: Python
from pyTooling.Warning import WarningCollector
class Caller:
def operation(self) -> None:
warnings = []
a = ClassA()
with WarningCollector(warnings) as warning:
a.methA()
print("Warnings:)
for warning in warnings:
print(f" {warning}")
|