English | 中文
Raise exceptions while creating exception classes on the fly, without the need to predefine them beforehand. For instance, if you want to raise an exception named NotUnderstandError
, simply import import gqylpy_exception as ge
and execute raise ge.NotUnderstandError
directly for convenience and efficiency.
pip3 install gqylpy_exception
import gqylpy_exception as ge
raise ge.AnError(...)
With gqylpy_exception
, you can create exception classes with arbitrary names. AnError
is not predefined in gqylpy_exception
; it is dynamically created when your code executes ge.
due to the magic method __getattr__
.
Alternatively, you can also create exceptions upon import:
from gqylpy_exception import AnError
raise AnError(...)
Lastly, gqylpy_exception
avoids duplicate creation of exception classes. Once an exception class has been created, it is stored in the ge.__history__
dictionary. When you attempt to create the same exception again, it will be retrieved from this dictionary.