Handy bug tracking using Error.Backtrace().Join("\n")

When debugging code catch errors and trace their source using the Error.Backtrace property.

Public Sub Form_Load()

' Your Code Here...

Catch
  Message.Error(Error.Text & "\nBacktrace:\n" & Error.Backtrace.Join("\n"))
End

Or...
Public Sub Form_Load()

' Your Code Here...

Catch
  Debug Error.Text & "\nBacktrace:\n" & Error.Backtrace.Join("\n")
End

This approach could be further improved by displaying the error in a dialog only for a debug compile - as demonstrated here, while the error is written to a log file for a release compile. The distinction between debug compilat and release compilat could be realized by a global static constant.

Public $bDEBUG As Boolean = False
Public $sPath As String = "~/gambas-log.txt"

Public Sub Form_Load()

' Your Code Here...

Catch
  Dim sError As String = Error.Text & "\nBacktrace:\n" & Error.Backtrace.Join("\n")
  If $bDEBUG Then
    Message.Error(sError)
  Else
    If LogToFile($sPath, "Error", "My-current-component", sError) = False Then
      ' Somenting really bad happened!
    Endif
  Endif
End


Public Function LogToFile(sPath As String, sCategory As String, sTopic As String, sMessage As String) As Boolean
  Dim hFile As File

  If Exist(sPath) Then
    hFile = Open sPath For Write Append
  Else
    hFile = Open sPath For Write Create
  Endif

  Print #hFile, Now; " | "; sCategory; " | "; sTopic; " | "; Replace$(Replace$(sMessage, "\n", "; "), ":;", ":")
  Close #hFile

  Return True

Catch
  Return False
End