Exceptionsin a Callback
17
try:
function()
except SystemExit:
raise
except:
netsvc.logException()
sys.exit()
The details of the exception are loggedw ith level" LOG_ERROR"and a specific log channel is not
specified. If you wanted to log the detailsof the exception to a specific log channel, or vary the level,
youc an use the "exceptionDetails()" function of the "netsvc" module to obtain the same in-
formationthat would be logged by the "logException()" function and then call the"notify()"
member function of an instance of the Logger class yourself.
try:
function()
except SystemExit:
raise
except:
details = netsvc.exceptionDetails()
logger.notifyChannel("WARNING",netsvc.LOG_WARNING,details)
pass
If you don’t want the stack trace and only want the description of the exception, use the function "ex-
ceptionDescription()" instead.The result of calling either of these functions need not be used
with the logger, but could be displayed using any other available mechanism as well.
Note that the "exceptionDetails()" and "exceptionDescription()" functions are also
available in the "netrpc" module if you are using that in a standalone client application.
Exceptions in a Callback
Whenever a callback is executed, it occurs as a result of a call from C++ code into Python code. Be-
cause of the mix of C++ code and Python code, if an exception occurs within the callback function,
Pythonc an’tby itselfproperly shutdown theapplication. This is further complicated by the fact tha t a
callback can be called within the context of a callback from the event dispatcher.
Asa consequenc e,whe n any callback into Python code from C++ occurs, if a Python exception occurs
andthe callback itselfdoesn’t catch it and deal with it, it will be caughtwith the details of the exception
being logged. The event dispatcher will then be stopped if it is running and the "SystemExit"ex-
ception raised in order to prevent Python from running any further code. The outcome is the same as
when only Python code is beingused, except that the details of the exception are displayed using the
logging facility rather than beingdumped directly onto the standard error output.