The XMLSpy API returns errors in two different ways. Every API method returns a HRESULT. This return value informs the caller about any malfunctions during the execution of the method. If the call was successful, the return value is equal to S_OK. C/C++ programmers generally use HRESULT to detect any errors.
VisualBasic, scripting languages and other high-level development environments, don't give the programmer access to the returning HRESULT of a COM call. They use the second error raising mechanism also supported by the XMLSpy API, the IErrorInfo interface. If an error occurs the API creates a new object that implements the IErrorInfo interface. The development environment takes this interface, and fills its own error handling mechansim with the provided informations.
The next paragraphs describes how to deal with errors raised from the XMLSpy API in different development environments.
VisualBasic
A common way to handle errors in VisualBasic is to define an error handler. This error handler can be set with the On Error statement. Usually the handler displays a error message and does some cleanup to avoid spare references and any kind of resource leaks. VisualBasic fills its own Err object with the information from the IErrorInfo interface.
Example:
Sub Validate()
'place variable declarations here
'set error handler
On Error GoTo ErrorHandler
'if IsValid() fails, program execution continues
'at ErrorHandler:
bValid = objSpy.ActiveDocument.IsValid(strMsg,nPos,objBadXMLData)
'additional code comes here
'exit
Exit Sub
ErrorHandler:
Set objBadXMLData = Nothing
MsgBox("Error: " & (Err.Number - vbObjectError) & Chr(13) &
"Description: " & Err.Description)
End Sub
JavaScript
The Microsoft implementation of JavaScript (JScript) provides a try - catch mechanism to deal with errors raised from COM calls. It is very similar to the VisualBasic approach, because you also declare an error object containing the necessary informations.
Example:
function Validate()
{
// please insert variable declarations here
try {
bValid = objSpy.ActiveDocument.IsValid(sMsg,nPos,objBad);
}
catch(Error) {
objBad = null;
sError = Error.description;
nErrorCode = Error.number;
return false;
}
return bValid;
}
C/C++
C/C++ gives you easy access to the HRESULT of the COM call and to the IErrorInterface.
HRESULT hr;
// call IsValid() from the XMLSpy API
if(FAILED(hr = ipDoc->IsValid(&varMsg,&varPos,&varBadData))) {
IErrorInfo *ipErrorInfo = NULL;
if(SUCCEEDED(::GetErrorInfo(0,&ipErrorInfo))) {
BSTR bstrDescr;
ipErrorInfo->GetDescription(&bstrDescr);
// handle error information
wprintf(L"Error message:\t%s\n",bstrDescr);
::SysFreeString(bstrDescr);
// release error info
ipErrorInfo->Release();
}
}
Previous
Top
Next