Resources

Handling Error Objects

You can catch and report error objects using either OLE DB or ADO. For details, see the following two sections:


Handling Errors Using ADO

The OLE DB error objects returned by the SAS Data Providers are presented in the ADO interface as Error objects. Because it is possible for more than one error to be returned, the errors are stored in the Errors Collection. To retrieve complete error information, you iterate through the Errors Collection and output the information. In most cases, the Description and Number properties of the Error object provide the most helpful information.

The Visual Basic, VBScript and ASP examples all show how errors can be handled using ADO. The VBScript and ASP examples perform the same tasks as the Visual Basic example but there are language differences between them. When reviewing the sample code, keep the following differences in mind:

Note: Although the examples use the Debug.Print and MsgBox methods to display error information, applications can use other methods such as Document.Write or Response.Write. You can also write an application that traps errors rather than displaying error information.

The following examples use the Local Data Provider to attempt to open a table named lostDataset.

If lostDataset does not exist in the directory c:\testdata, the application generates an error and outputs the error information. The "Description" property used in the examples is typically a string message composed by the SAS data provider. However, it may also be a message from the server or some component used by the data provider. The "Number" property is typically the HRESULT value returned by the underlying OLE DB interface method. In the examples, the value is output in hexadecimal because that is the way that it is typically written in the header files such as WinError.h and OleDBErr.h.

Visual Basic Error-Handling Example


Sub Main()
  Dim cn As ADODB.Connection
  Dim rs As ADODB.Recordset
  Dim errorObject As ADODB.Error
  
  Set cn = New ADODB.Connection
  Set rs = New ADODB.Recordset
  
  On Error GoTo DisplayErrorInfo
  cn.Provider = "sas.localprovider.1"
  cn.Properties("Data Source") = "c:\testdata"

  cn.Open
  rs.Open "lostDataset", cn, adOpenDynamic, adLockOptimistic, ADODB.adCmdTableDirect
  rs.Close
  cn.Close

DisplayErrorInfo:
  For Each errorObject In rs.ActiveConnection.Errors
    Debug.Print "Description :"; errorObject.Description
    Debug.Print "Number:"; Hex(errorObject.Number)
  Next
End Sub

VBScript Error-Handling Example


<HTML>
Example showing error handling using VBScript.

<SCRIPT LANGUAGE=VBSCRIPT>
Const adOpenDynamic = 2
Const adLockOptimistic = 3
Const adCmdTableDirect = 512

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Set errorObject = CreateObject("ADODB.Error")

On Error Resume Next
cn.Provider = "sas.localprovider.1"
cn.Properties("Data Source") = "c:\testdata"

cn.Open
rs.Open "lostDataset", cn, adOpenDynamic, adLockOptimistic, adCmdTableDirect
DisplayErrorInfo
rs.Close
cn.Close


sub DisplayErrorInfo()
  For Each errorObject In rs.ActiveConnection.Errors
    MsgBox "Description: " & errorObject.Description & Chr(10) & Chr(13) & _
           "Number:      " & Hex(errorObject.Number)
  Next
End Sub
</SCRIPT>
</HTML>

ASP Error-Handling Example

<HTML>
Example showing error handling using VBScript.

<%
Const adOpenDynamic = 2
Const adLockOptimistic = 3
Const adCmdTableDirect = 512

Set cn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")
Set errorObject = Server.CreateObject("ADODB.Error")

On Error Resume Next
cn.Provider = "sas.localprovider.1"
cn.Properties("Data Source") = "c:\testdata"

cn.Open
rs.Open "lostDataset", cn, adOpenDynamic, adLockOptimistic, adCmdTableDirect
DisplayErrorInfo
rs.Close
cn.Close

sub DisplayErrorInfo()
  For Each errorObject In rs.ActiveConnection.Errors
    Response.Write "Description: " & errorObject.Description & Chr(10) & Chr(13) & _
           "Number:      " & Hex(errorObject.Number)
  Next
End Sub 
%>
</SCRIPT>
</HTML>

Handling Errors Using OLE DB

The SAS Data Providers support OLE DB error objects, which are an extension of automation error objects. Most interfaces implemented by the providers return error objects. For any component, use the ISupportErrorInfo interface to determine which interfaces on the component return error objects.