The <try> / <catch>, <throw>, and <rethrow> elements 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" standalone="no"?>
<!DOCTYPE stax SYSTEM "stax.dtd">

<stax>

  <defaultcall function="main"/>

  <signalhandler signal="'STAXPythonEvaluationError'">
    <sequence>
      <script>
        # This signal handler overrides the default signalhandler and
        # raises STAX exception 'PythonException' instead of terminating
        # the STAX job
        errorMsg = 'STAXPythonEvaluationError signal raised. Terminating job. %s' % \
                   (STAXPythonEvalMsg)
      </script>
      <throw exception="'PythonException'">errorMsg</throw>
    </sequence>
  </signalhandler>

  <function name="main">
    <try>
      <sequence>
        <call function="'foo'"/>
      </sequence>
      <catch exception="'PythonException'" typevar="exceptionType" var="eInfo">
        <log message="1">'Handler: main, Type: %s, Info: %s' % (exceptionType, eInfo)</log>
      </catch>
    </try>
  </function>

  <function name="foo">
    <sequence>

      <parallel>

        <sequence>

          <log message="1">'Element 1 running in parallel'</log>

          <script>
            # Do whatever you wanted to do
            # I'll just cause a Python exception for now
            x = abc
          </script>

        </sequence>

        <sequence>

          <log message="1">'Element 2 running in parallel'</log>
          <!-- Do whatever you wanted to do in parallel -->

        </sequence>

      </parallel>
   
    </sequence>
  </function>
 
</stax>

Here's an example of a STAX job that uses a Python try/catch block to catch a Python exception and then raises a STAX exception when this occurs.:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE stax SYSTEM "stax.dtd">

<stax>

  <defaultcall function="main"/>

  <function name="main">
    <sequence>

      <try>
        <sequence>
          <call function="'foo'"/>
        </sequence>
        <catch exception="'PythonException'" typevar="exceptionType" var="eInfo">
          <log message="1">'Handler: main, Type: %s, Info: %s' % (exceptionType, eInfo)</log>
        </catch>
      </try>

    </sequence>
  </function>

  <function name="foo">
    <sequence>

      <parallel>

        <sequence>

          <log message="1">'Element 1 running in parallel'</log>

          <script>
            pythonExceptionMsg = None

            try:
              # Do whatever you wanted to do
              # I'll just cause a Python exception for now
              x = abc
            except:
              # Catches all Python exceptions
             import sys
             pythonExceptionMsg = 'Python Exception in function foo: [%s]\n  Details: [%s]' % \
               (sys.exc_type, sys.exc_value)
          </script>

          <if expr="pythonExceptionMsg != None">
            <throw exception="'PythonException'">pythonExceptionMsg</throw>
          </if>

        </sequence>

        <sequence>

          <log message="1">'Element 2 running in parallel'</log>
          <!-- Do whatever you wanted to do in parallel -->

        </sequence>

      </parallel>
   
    </sequence>
  </function>

</stax>  

--------------------------------------------------------------
Sharon Lucas
IBM Austin,   lucass@us.ibm.com
(512) 838-8347 or Tieline 678-8347



Yuan Zhengcai <yuanzc@nec-labs.com>
Sent by: staf-users-admin@lists.sourceforge.net

02/07/2006 09:42 AM

To
staf-users@lists.sourceforge.net
cc
Subject
[staf-users] How can STAX script catch exception thrown by python thread?






Hi,
Did anybody use STAX script to catch exeption thrown by python thread?
Or how to convert python exception into STAX exception?

For example:
<function name="main">
        <sequence>
              ...
             <try>
                   <sequence>
                           <call function="'foo'"/>
                            ........
                            .......
                  </sequence>
                  <catch exception="'...'">
                           <log>"catch exception." </log>
                  </catch>
             </try>
       </sequence>
</function>

<function name="foo">
        <sequence>
                <script>
                                 import threading
                                 import thread
                                 class PrintThread(threading.Thread):
                                                  def run(self):
                                                             ...............
                                                             ..............
                                                             thread.interrupt_main()
                                 
                                  testThread = PrintThread()
                                  testThread.start()
                </script>
        </sequence>
</function>

Can I catch exception thrown by PrintThread in function
"main" ?

Thanks.
------








-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
staf-users mailing list
staf-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/staf-users