Logging, Exceptions, and Errors

From Phidgets Support
Revision as of 19:58, 30 October 2018 by Jdecoux (talk | contribs)

You've written your code, fixed the compiler errors, and yet, the program still isn't behaving as intended. The tools described on this page will help you further debug your program and figure out where in the code things are going wrong.

If you haven't read it yet, we recommend first reading the page on Phidget Programming Basics to better understand the contents of this page.

Errors and Error Events

The Phidget library uses errors and error events to communicate information about potential problems in your program. These can happen in-line in the form of an exception or error code (which generally indicate a problem in the code), and as Error Events (which generally indicate a problem with the Phidget's environment).

Errors from Function Calls

When a call to a Phidgets function fails it will throw an exception (or return an error code, depending on the language). It is important to check each function call for errors, and to catch and handle any errors.

Common causes of this type of errors are the Phidget not being attached, or one of the values provided to the function being outside the valid range.

You can handle exceptions and return codes as follows:

try {
    ch.open(5000);
} catch (PhidgetException ex) {
    System.out.println("Failed to open: " + ex.getMessage());
}
try:
    ch.openWaitForAttachment(5000);
except PhidgetException as e:
    print("Failed to open: " + e.details)
try
{
    ch.Open(5000);
}
catch (PhidgetException ex)
{
    Console.WriteLine("Failed to open: " + ex.Description)
}
PhidgetDigitalInputHandle ch;
PhidgetReturnCode res;
...    
res = Phidget_openWaitForAttachment((PhidgetHandle)ch, 5000);
if (res != EPHIDGET_OK) {
    char* desc;
    Phidget_getErrorDescription(res, &desc);
    printf("Failed to open: (%d) %s\n", res, desc);
}

The consequences of not handling errors correctly ranges from improper behavior, to the program crashing due to an unhandled exception. Programs should check for errors on each function call, and handle any errors.

Error Codes

A list of error codes can be found in the Phidget22 API.

Error Events

Error events are asynchronously generated by Phidget devices, and by the Phidget library itself.

These do not necessarily indicate a problem with your program, and often serve as a status update from your device that your program should know. For example, a DistanceSensor might send an OutOfRange error if it does not detect anything in its field of view, or a TemperatureSensor could send a Saturation error indicating its temperature reading is outside the valid range for the sensor.

Error events are also fired when network errors occur, or when the library is unable to handle incoming change events quickly enough (event handlers could be too slow).

We strongly recommend setting up an error event handler for your channels so your program can keep track of these error conditions.

//Declare the event listener
public static ErrorListener onError = new ErrorListener() {
    @Override
    public void onError(ErrorEvent e) {
        System.out.println("Code: " + e.getCode());
        System.out.println("Description: " + e.getDescription());
    }
};
...
//Declare your object. Replace "DigitalInput" with the object for your Phidget.
DigitalInput ch;
...
//Assign the event listener that will be called when the event occurs
ch.addErrorListener(onError);
#Declare the event handler
def onErrorHandler(self, code, description):
    print("Code: " + str(code))
    print("Description: " + description)
...
#Declare your object. Replace "DigitalInput" with the object for your Phidget
ch = DigitalInput()
...
#Assign the handler that will be called when the event occurs
ch.setOnErrorHandler(onErrorHandler)
//Declare the event handler
void error(object sender, Phidget22.Events.ErrorEventArgs e) {
    Console.WriteLine("Code: " + e.Code.ToString());
    Console.WriteLine("Description: " + e.Description);
}
...
//Declare your object. Replace "DigitalInput" with the object for your Phidget.
DigitalInput ch;
...
//Assign the handler that will be called when the event occurs
ch.Error += error;
//Declare the event handler
static void CCONV onErrorHandler(PhidgetHandle ph, void *ctx, Phidget_ErrorEventCode code, const char* description) {
    printf("Code: %d\n", code);
    printf("Description: %s\n", description);
}
...
//Declare your object. Replace "PhidgetDigitalInputHandle" with the handle for your Phidget object.
PhidgetDigitalInputHandle ch;
...
//Assign the handler that will be called when the event occurs
Phidget_setOnErrorHandler((PhidgetHandle)ch, onErrorHandler, NULL);

Full descriptions of all error event codes can be found in the Error Event Code List page.

Logging

You can enable logging to get more debugging information. This would happen at the very start of your program, before even initializing your software object or opening it. Logging lets you get feedback from the Phidget libraries about things happening behind the scenes.

PhidgetLog_enable(PHIDGET_LOG_INFO, NULL);

Or in Java:

Log.enable(LogLevel.INFO, null);

When NULL is passed to enable() in the above examples, the logging system will output to STDERR.

For a more comprehensive look at the logging system in Phidget22, you can check out the Logging Details page.

Other Problems

If your Phidget is still not behaving as expected after handling errors and exceptions, have a look at our General Troubleshooting guide to track down the problem.

Further Reading

Phidget Programming Basics - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.

Data Interval/Change Trigger - Learn about these two properties that control how much data comes in from your sensors.

Using Multiple Phidgets - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.

Polling vs. Events - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.

Phidget Network Server - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.

Best Phidgets Practices - Good programming habits that will save you from common problems when writing code for your Phidgets.