Python 7.0pl5 Benutzerhandbuch

Seite von 124
Exceptions in a Callback
17
try:
function()
except SystemExit:
raise
except:
netsvc.logException()
sys.exit()
The details of the exception are logged with level "
LOG_ERROR
" and a specific log channel is not
specified. If you wanted to log the details of the exception to a specific log channel, or vary the level,
you can use the "
exceptionDetails()
" function of the "
netsvc
" module to obtain the same in-
formation that 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,
Python can’t by itself properly shutdown the application. This is further complicated by the fact that a
callback can be called within the context of a callback from the event dispatcher.
As a consequence, when any callback into Python code from C++ occurs, if a Python exception occurs
and the callback itself doesn’t catch it and deal with it, it will be caught with 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 being used, except that the details of the exception are displayed using the
logging facility rather than being dumped directly onto the standard error output.