Just like Python, HALCON/Python handles errors via exceptions.
def load_custom_pcb_img_or_fallback(custom_pcb_filename): try: return ha.read_image(custom_pcb_filename) except ha.HOperatorError as err: if err.error_code != 5200: # HALCON error code File not found. raise err return ha.read_image('pcb') img = load_custom_pcb_img_or_fallback('does-not-exist')
In this example, we want to either load a custom PCB image or fallback to the default PCB image if the image cannot be found. If another error occurs, we want to propagate the original error further up the call stack.
The custom exceptions used by HALCON/Python form an inheritance hierarchy:
Exception | | v HError |------------------|------------------|---------------| | | | | v v v v HOperatorError HTupleConversionError HDevEngineError HInteropError
It starts with the standard Python Exception, from which HError inherits, followed by four more specific exceptions that inherit from HError. They occur in different situations. Operators and other functions may raise more than one type of exception.
class CustomClass(object): pass img = ha.read_image(CustomClass())
HALCON/Python does not know how to pass the custom user defined class to the HALCON operator read_image. For any instance where a conversion to or from a native HALCON tuple fails, an instance of HTupleConversionError is raised.
All custom HALCON/Python exceptions implement __str__.