Add example showing Python exception handling
This commit is contained in:
@ -7,6 +7,27 @@ current Python process with an error message. C++ exceptions allow capturing
|
|||||||
them on the C++ side and rethrowing them on the Python side. This way
|
them on the C++ side and rethrowing them on the Python side. This way
|
||||||
LAMMPS errors can be handled through the Python exception handling mechanism.
|
LAMMPS errors can be handled through the Python exception handling mechanism.
|
||||||
|
|
||||||
|
.. code-block:: Python
|
||||||
|
|
||||||
|
from lammps import lammps, MPIAbortException
|
||||||
|
|
||||||
|
lmp = lammps()
|
||||||
|
|
||||||
|
try:
|
||||||
|
# LAMMPS will normally terminate itself and the running process if an error
|
||||||
|
# occurs. This would kill the Python interpreter. To avoid this, make sure to
|
||||||
|
# compile with LAMMPS_EXCEPTIONS enabled. This ensures the library API calls
|
||||||
|
# will not terminate the parent process. Instead, the library wrapper will
|
||||||
|
# detect that an error has occured and throw a Python exception
|
||||||
|
|
||||||
|
lmp.command('unknown')
|
||||||
|
except MPIAbortException as ae:
|
||||||
|
# Single MPI process got killed. This would normally be handled by an MPI abort
|
||||||
|
pass
|
||||||
|
except Exception as e:
|
||||||
|
# All (MPI) processes have reached this error
|
||||||
|
pass
|
||||||
|
|
||||||
.. warning::
|
.. warning::
|
||||||
|
|
||||||
Capturing a LAMMPS exception in Python can still mean that the
|
Capturing a LAMMPS exception in Python can still mean that the
|
||||||
|
|||||||
Reference in New Issue
Block a user