by Robert Einhorn (1 Submission)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Mon 10th August 2009
Date Added: Mon 8th February 2021
Rating:
(1 Votes)
Try-Catch feature for VB6, VBA, VBScript
Private Sub Main()
Dim lngTest, lngZero As Long
With New Try: On Error Resume Next
lngTest = 1 / lngZero 'Division by zero
.Catch: On Error GoTo 0: Select Case .Number
Case 11
MsgBox "Division by zero error."
Case Is <> 0
MsgBox .Description
End Select: End With
End Sub
'****************************************************************
'* CLASS: Try
'* This class stores properties of the Err object.
'* Budapest, 2008 Feb. 25. Robert Einhorn
'* https://sites.google.com/site/truetryforvisualbasic/
'****************************************************************
Private mstrDescription As String
Private mlngHelpContext As Long
Private mstrHelpFile As String
Private mlngLastDllError As Long
Private mlngNumber As Long
Private mstrSource As String
Public Sub Catch()
mstrDescription = Err.Description
mlngHelpContext = Err.HelpContext
mstrHelpFile = Err.HelpFile
mlngLastDllError = Err.LastDllError
mlngNumber = Err.Number
mstrSource = Err.Source
End Sub
Public Property Get Source() As String
Source = mstrSource
End Property
Public Property Get Number() As Long '*** Default property ***
Number = mlngNumber
End Property
Public Property Get LastDllError() As Long 'Missing in VBScript
LastDllError = mlngLastDllError
End Property
Public Property Get HelpFile() As String
HelpFile = mstrHelpFile
End Property
Public Property Get HelpContext() As Long
HelpContext = mlngHelpContext
End Property
Public Property Get Description() As String
Description = mstrDescription
End Property