In the case of both Microsoft® Visual Basic® and Java, if an error occurs within the method, the method will throw an exception. The script or program must check for these exceptions separately from the return value.
For C++, the return value is always an HRESULT, and indicates whether the method call succeeded or failed. If the call failed then the return value indicates why it failed.
Microsoft® Visual Basic® VBScript uses the Err object's Number property to check for errors, as shown in the example code that follows.
The properties of the Err object are set by the generator of an error — Visual Basic, an OLE object, or the Visual Basic programmer. The default property of the Err object is Number. Err.Number contains an integer and can be used by an Automation object to return an SCode.
When a run-time error occurs, the properties of the Err object are filled with information that uniquely identifies the error and information that can be used to handle it. To generate a run-time error in your code, use the Raise method.
The Err object's properties are reset to zero or zero-length strings ("") after a On Error Resume Next statement and after an Exit Sub or Exit Function statement within an error-handling routine. The Clear method can be used to explicitly reset Err.
The Err object is an intrinsic object with global scope — there is no need to create an instance of it in your code. The following is an example using a function from the Microsoft® CryptoAPI 2.0:
Set CertObj = StoreObj.FindCertificate(0, 0, &H50000, _
BlobApi.I4ToBlobString(2), Nothing, CertObj)
If err.Number <> 0 Then
If err.Number <> &H80092111 Then
MyWriteLn "FindCertificate err = " &Hex(err.Number)
End If
GetSignCert = Null
MyWriteLn "No Signer Cert"
Exit Function
End If
ComFailException indicates a failure. The exception object wraps an HRESULT, the return type for most methods in the Component Object Model (COM). The default value of the HRESULT stored in a ComFailException is E_FAIL (0x80004005L). All failure HRESULTs have their most significant bit set.
The values of system-defined HRESULTs can be found in WINERROR.H, included with the Microsoft® Platform SDK. For object specific errors, see the documentation associated with the object.
For system-defined errors, the detail message stored in the ComException object is the string returned by the Microsoft Win32® API function FormatMessage, which returns a brief message associated with the error. This detail message can be retrieved by calling getMessage (defined by the Throwable class).
The following code is an example of testing the ComFailException using a function from the Microsoft® CryptoAPI 2.0:
try {
certObj = (ICertificate) storeObj.FindCertificate(0, 0,
0x50000, blobApi.I4ToBlobString(2), null, certObj);
} catch (ComFailException e) {
if (e.getHResult() != 0x80092111)
TextIO.writeLn("FindCertificate err = " +
TextIO.toUString(e.getHResult()));
TextIO.writeLn("No Signer Cert");
return null;
}
C++ directly returns HRESULT values that can be used for error checking, as the following code fragment demonstrates. For example error codes, see the list of error codes in the "Common HRESULT Values" section that follows.
HRESULT hr;
BSTR strAttributeName = SysAllocString("TheAttribute");
BSTR strAttributeValue = SysAllocStringByteLen("", 49);
hr = pICertServerPolicy->GetRequestAttribute(
strAttributeName,
&strAttributeValue);
if(S_OK != hr) // If the function failed…
{
if(E_INVALIDARG == hr);
; // Do something
}
The following table lists the values of common HRESULT values.
Name | Description | Value |
---|---|---|
S_OK | Operation successful | 0x00000000 |
E_UNEXPECTED | Unexpected failure | 0x8000FFFF |
E_NOTIMPL | Not implemented | 0x80004001 |
E_OUTOFMEMORY | Failed to allocate necessary memory | 0x8007000E |
E_INVALIDARG | One or more arguments are invalid | 0x80070057 |
E_NOINTERFACE | No such interface supported | 0x80004002 |
E_POINTER | Invalid pointer | 0x80004003 |
E_HANDLE | Invalid handle | 0x80070006 |
E_ABORT | Operation aborted | 0x80004004 |
E_FAIL | Unspecified failure | 0x80004005 |
E_ACCESSDENIED | General access denied error | 0x80070005 |
E_NOTIMPL | Not implemented | 0x80000001 |