11.4   Error Handling

The .NET programming languages each offer a mechanism for error handling. In C# and managed C++, you use try... catch blocks. Within this standard mechanism, HALCON/.NET offers its special exceptions:

The following code shows how to catch the error that occurs when the operator ReadImage is called with a wrong image file name. Then, a message box is shown that displays the error code in the caption and the HALCON error message:

HImage Image;

try
{
  Image = new HImage("unknown");
}
catch (HOperatorException exception)
{
  MessageBox.Show(exception.Message, "HALCON error # " + exception.GetErrorCode());
}

All HALCON error codes and their corresponding error messages are summarized in the Extension Package Programmer's Manual.

The HSmartWindowControl and HSmartWindowControlWPF provide the event HErrorNotify. This event allows the user to react to errors that take place internally within the control, but can have external causes, like for example an unplugged dongle or a missing license file.