Berikut Struktur penulisan dari try catch di VB .NET:
Try
' Do something in here that
' might raise an error.
Catch
' Handle exceptions that occur within
' the Try block, here.
Finally
' Perform cleanup code in here.
End Try
End Sub
Contoh penggunaan error handling:
Sub ReadTable()
Dim connection As SqlConnection
Try
connection = New SqlConnection("connection_string")
connection.Open()
Dim command As SqlCommand = connection.CreateCommand
command.CommandText = "SELECT NamaField FROM NamaTabel"
Dim reader As SqlDataReader = command.ExecuteReader
While reader.Read
Dim connection As SqlConnection
Try
connection = New SqlConnection("connection_string")
connection.Open()
Dim command As SqlCommand = connection.CreateCommand
command.CommandText = "SELECT NamaField FROM NamaTabel"
Dim reader As SqlDataReader = command.ExecuteReader
While reader.Read