API and Command Error Handling

<< Click to Display Table of Contents >>

Navigation:  NxLib API > HowTo's >

API and Command Error Handling

API and Command Errors

When using NxLib you can encounter two different types of errors:

API errors: these happen when a read or write operation to a tree item fails. This might happen when you try to modify a read-only item (like the camera's serial number of status node), or you try to read a value from an inexistent item (i.e. the path specified in the read operation does not correspond to a node in the tree).
API errors are reported directly via return codes or exceptions, depending on which of the overloaded read/write functions you use.

Command execution errors: the execution of commands via the Execute node at the tree root level is a concept that is built on top the tree read/write functionality. A command is executed after writing its name into the Command node. A separate thread takes care of the command execution and clears the Command node (sets it to the JSON value Null) after the command has finished executing. If the command execution thread encounters problems during the command it will create a node /Execute/Result/ErrorSymbol and /Execute/Result/ErrorText. ErorrSymbol serves as a unique, constant identifier for the error type and ErrorText contains an English human readable explanation of what happened. The user should therefore check for the existence of the ErrorSymbol node after each command execution to determine if the command suceeded. When using the NxLibCommand classes to execute commands the existence of ErrorSymbol is checked automatically and an exception with the error code NxLibExecutionFailed is generated.

Error Handling with Exceptions

Using our C++ or C# classes error handling can conveniently done with exceptions by encapsulating code blocks like this:

hmtoggle_plus1C++
hmtoggle_plus1Halcon/HDevelop

Error Handling with Return Codes

It is also possible to handle errors by checking return codes of each NxLib function call.

A list of return codes of the read/write functions can be found in the API Return Codes topic.

Reading/Writing Tree Nodes

For the case of reading or writing a tree error handling could look like this:

hmtoggle_plus1C/C++

Command Execution

hmtoggle_plus1C/C++