9.3. Error handlingWhen you implement a program which must interact with USB modules, you cannot disregard errorhandling. Inevitably, there will be a time when a user will have unplugged the device, either beforerunning the software, or even while the software is running. The Yoctopuce library is designed to helpyou support this kind of behavior, but your code must nevertheless be conceived to interpret in thebest possible way the errors indicated by the library.The simplest way to work around the problem is the one used in the short examples provided in thischapter: before accessing a module, check that it is online with the isOnline function, and thenhope that it will stay so during the fraction of a second necessary for the following code lines to run.This method is not perfect, but it can be sufficient in some cases. You must however be aware thatyou cannot completely exclude an error which would occur after the call to isOnline and whichcould crash the software. The only way to prevent this is to implement one of the two error handlingtechniques described below.The method recommended by most programming languages for unpredictable error handling is theuse of exceptions. By default, it is the behavior of the Yoctopuce library. If an error happens whileyou try to access a module, the library throws an exception. In this case, there are three possibilities:• If your code catches the exception and handles it, everything goes well.• If your program is running in debug mode, you can relatively easily determine where theproblem happened and view the explanatory message linked to the exception.• Otherwise... the exception makes your program crash, bang!As this latest situation is not the most desirable, the Yoctopuce library offers another possibility forerror handling, allowing you to create a robust program without needing to catch exceptions at everyline of code. You simply need to call the YAPI.DisableExceptions() function to commute thelibrary to a mode where exceptions for all the functions are systematically replaced by specific returnvalues, which can be tested by the caller when necessary. For each function, the name of eachreturn value in case of error is systematically documented in the library reference. The name alwaysfollows the same logic: a get_state() method returns a Y_STATE_INVALID value, aget_currentValue method returns a Y_CURRENTVALUE_INVALID value, and so on. In anycase, the returned value is of the expected type and is not a null pointer which would risk crashingyour program. At worst, if you display the value without testing it, it will be outside the expectedbounds for the returned value. In the case of functions which do not normally return information, thereturn value is YAPI_SUCCESS if everything went well, and a different error code in case of failure.When you work without exceptions, you can obtain an error code and an error message explainingthe source of the error. You can request them from the object which returned the error, calling theerrType() and errMessage() methods. Their returned values contain the same information asin the exceptions when they are active.9.4. Integration variants for the C++ Yoctopuce libraryDepending on your needs and on your preferences, you can integrate the library into your projects inseveral distinct manners. This section explains how to implement the different options.Integration in source formatIntegrating all the sources of the library into your projects has several advantages:• It guaranties the respect of the compilation conventions of your project (32/64 bits, inclusion ofdebugging symbols, unicode or ASCII characters, etc.);• It facilitates debugging if you are looking for the cause of a problem linked to the Yoctopucelibrary;• It reduces the dependencies on third party components, for example in the case where youwould need to recompile this project for another architecture in many years;• It does not require the installation of a dynamic library specific to Yoctopuce on the finalsystem, everything is in the executable.9. Using Yocto-GPS with C++64 www.yoctopuce.com