### Use Try Catch exception handling

The Try .. Catch code block to use is
Try
' Code
Catch
' handle Error, like
Log("ERROR: " & LastException.Message)
End Try
Common Practice
Good common practice is to use a boolean to return the result and handle accordingly.
`Sub My As Boolean
Dim result As Boolean = False
Try
'Define steps
result = True
Catch
Log(LastException.Message)
End Try
Return result
End Sub`
Another option is to return a string. If empty then result is fine else show the error message.
`Sub My As String
Dim result As String = ""
Try
'Define steps
Catch
result = LastException.Message
End Try
Return result
End Sub`
Examples
Get a list of tables from a SQLite database
' Get the table list from the SQLite Master Table
' Returns: List of Tablenames
`Sub getTableList As List
Dim l As List
Dim cursor As ResultSet
l.Initialize
Log("*** List of Tables ***")
Try
cursor = SQL.ExecQuery ("SELECT name FROM SQLITE_MASTER WHERE Type='table' ORDER BY name;")
Do While cursor.NextRow
For i = 0 To cursor.ColumnCount - 1
Log(cursor.GetString2(i))
l.Add(cursor.GetString2(i))
Next
Loop
labelInfo.Text= "Number of Tables found: " & l.Size
Log("----------------------")
Catch
Log("ERROR: " & LastException.Message)
End Try
Return l
End Sub`
Opena serial port
'Init the serial port
'Returns true if successfull else false
`Sub SerialPortInit As Boolean
Dim result As Boolean = False
Try
Dim BaudRate As Int = 9600
Dim DataBits As Int = 8
Dim StopBits As Int = 1
Dim Parity As Int = 0
sp.Initialize("")
sp.Open("COM3")
sp.SetParams (BaudRate , DataBits , StopBits , Parity )
result = True
Catch
Log(LastException.Message)
End Try
Return result
End Sub`

Changed: , Category: Try Catch Exception, File: T6C273M2029.htm, ID: 2029