Platform SDK: DirectX

Visual Basic Error Trapping

[C++]

This topic pertains only to applications written in Visual Basic.

[Visual Basic]

In C and C++, HRESULT values are returned by every method call. The application can choose to examine and act on return values, or it can ignore them.

Error handling in DirectX for Visual Basic is very different. In Visual Basic a method call either succeeds or it fails. Visual Basic has no success codes equivalent to positive HRESULT values such as S_FALSE. If the call fails, an error is raised, and unless the application has an error handler, execution terminates. The error number is set in the global Visual Basic Err object. Most of the possible errors for each method are listed on the reference page for the method in this documentation.

In the following example, gObjDPlay is a DirectPlay4 object. If the call to DirectPlay4.CreatePlayer fails, execution continues at the FAILEDCREATEPLAYER label.

Function GetPlayerID(PlayerName As String, PlayerHandle As String) _
        as Long
 
  On Error GoTo FAILEDCREATEPLAYER
  GetPlayerID = gObjDPlay.CreatePlayer(PlayerName, PlayerHandle, 0, 0)
  On Error GoTo 0
 
  ' Any code in this section is run only if CreatePlayer succeeded.
  .
  .
  .
  Exit Function
 
FAILEDCREATEPLAYER:
 
  If Err.Number = DPERR_INVALIDPLAYER Then 
      MsgBox ("Not a valid player.")
  Else
      MsgBox("Unidentified player creation error.")
  End If
  GetPlayerID = 0
 
End Function