Error handling
When using a Zaber device and this library, a number of different exceptional situations may occur. These range from an invalid method argument to various device warnings or failures. An example of a situation is if a device loses power or becomes unresponsive for a timeout period, or if an axis stalls while moving.
This library provides a system to handle these situations.
In the following example, the device is powered off during the execution of the program, an exception is thrown by the library to indicate this situation, and the program catches it.
# from zaber_motion.ascii import Connection
# from zaber_motion import Units, MotionLibException
try:
axis.move_absolute(1, Units.LENGTH_CENTIMETRES)
except MotionLibException as err:
print(err)
# Outputs:
# RequestTimeoutException: Device has not responded in given timeout
You may utilize the subclasses of
MotionLibException
(e.g. RequestTimeoutException) to further distinguish between various errors.
Before doing this, ensure that you import the exception subclass at the top of your script:
from zaber_motion import RequestTimeoutException
Additionally, some exceptions have property
details
that provides details on the cause of the exception and data from device's reply.
# from zaber_motion.ascii import Connection
# from zaber_motion import Units, MovementFailedException
try:
axis.move_absolute(1, Units.LENGTH_CENTIMETRES)
except MovementFailedException as err:
print(err.details.reason)
Consult the API Reference for a full list of exceptions and their details.