The <try> / <catch>, <throw>, and <rethrow>
deal with the processing of STAX exceptions (not Python exceptions). The
STAX <catch> element is used to catch STAX exceptions that you can
throw using the STAX <throw> element.
To catch Python exceptions, you use
the normal Python exception handling code to do so.
>> Can I catch exception thrown by PrintThread
in function "main" ?
You could use normal Python exception
handling code (see Python/Jython documentation for more information) to
catch a Python exception and you could use the STAX <throw> element
to throw an exception if a Python exception occurs. Note that when
a Python exception occurs that you don't explicitly catch, a STAXPythonEvaluationError
signal is raised and information about the error is logged and the STAX
job is terminated. Another option is to override the STAXPythonEvaluationError
signal via a <signalhandler> and have it <raise> a STAX exception.
In the STAX job you provided, you didn't see this because the Python
exception was occurring in another Python thread that you were starting
in your STAX job. This is not a good idea as the STAX job won't know
when the Python thread has completed. Instead, if you want to run
things in parallel in a STAX job, you should use the STAX <parallel>
element to start a separate STAX threads for each element within a <parallel>
element. I would need more information on what you're trying to accomplish
in this STAX job to advise you further.
Here's an example of a STAX job that
overrides the default signal handler for a STAXPythonEvaluationError signal
to throw a STAX exception instead of logging the error and terminating
the STAX job. It also then uses a Python try/catch block to catch
the STAX exception in the main function and log the exception.
<?xml version="1.0" encoding="UTF-8"
<!DOCTYPE stax SYSTEM "stax.dtd">
# This signal
handler overrides the default signalhandler and
# raises STAX
exception 'PythonException' instead of terminating
# the STAX
'STAXPythonEvaluationError signal raised. Terminating job. %s' % \
message="1">'Element 1 running in parallel'</log>
pythonExceptionMsg = None
# Do whatever you wanted to do
# I'll just cause a Python exception for now
x = abc
# Catches all Python exceptions
pythonExceptionMsg = 'Python Exception in function foo: [%s]\n Details:
[%s]' % \
Can I catch exception thrown by PrintThread in function
This SF.net email is sponsored by: Splunk Inc. Do you grep through log
for problems? Stop! Download the new AJAX search engine that
searching your log files as easy as surfing the web. DOWNLOAD
staf-users mailing list