Error trapping and Try/Catch/Finally

J Trahair

Well-known member
Joined
May 14, 2008
Messages
175
Location
Spain
Programming Experience
10+
Hi everyone.
In VB6 there was On Error Resume Next which crashed through a procedure from top to bottom, errors or no.
Is there the same in Try/Catch?
Eg.
Public Sub ConvertScreenFont()
Dim i As Long

On Error Resume Next

For i = 0 To Me.Controls.Count - 1
Me.Controls(i).FontName = "Arial"
Next i

End Sub

In other words, some controls don't have a FontName but I don't care!

Also, what is the name of the exception when the user clicks Cancel on the Printer dialog? As in:
Catch e As CancelPrintRunException

Thanks in advance.
 
When iterating through an array or collection of controls, you really should be doing a TypeOf and a DirectCast for this.

As in:
VB.NET:
For Each Ctrl As Control In Me.Controls
  If TypeOf Ctrl Is TextBox Then
    DirectCast(Ctrl, TextBox).FontName = "Arial"
  End If
  If TypeOf Ctrl Is Label Then
    DirectCast(Ctrl, Label).FontName = "Arial"
  End If
Next Ctrl

However you could still brute force it if you really, really, really want to:
VB.NET:
Try
  For Each Ctrl As Control In Me.Controls
    Ctrl.FontName = "Arial"
  Next Ctrl
Catch
  Resume
End Try
 
Back
Top