Handling GPS ToolKit Errors
GPS ToolKit notifies your application of any errors it encounters through the IErrorInfo interface. Because this is the standard method to communicate error information from a COM object, Visual Basic, Visual C++, and other development environments have built in support for this interface.
Note that, for simplicity, the examples in the reference section do not include error handling constructs.
Visual Basic
GPS ToolKit errors are handled in Visual Basic using the familiar On Error Goto construct. When an error occurs, detailed information can be found in the application's global Err object. For example, the following code traps an error and displays a description in a message box:
Private Sub MySub()
On Error GoTo MyErrorHandler
myGPSToolKit.Open "COM1"
Exit Sub
MyErrorHandler:
MsgBox Err.Description
End Sub
Visual C++
The wrapper class that Visual Studio creates when adding GPS ToolKit to your project uses the older MFC TRY/CATCH/END_CATCH exception handling macros instead of the C++ try/catch constructs. This means that in most cases, you will want to use the same TRY/CATCH/END_CATCH macros when trapping for GPS ToolKit exceptions.
The following example catches any errors and displays a description of the error in a message box:
void MyFunc()
{
TRY
{
m_myGPSToolKit.Open("COM1");
}
CATCH(CException, e)
{
char lpszError[1024];
e->GetErrorMessage(lpszError, 1024);
AfxMessageBox(lpszError);
}
END_CATCH
}
If you are not using GPS ToolKit through the automatically generated wrapper class, you may use the normal C++ try/catch construction to trap errors.
|