'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
LogError(Error.Message, StackTrace)
Return True
End Sub
Sub LogError(strErrorMsg As String, strStackTrace As String)
Dim str As String
Dim strCurrentDateTime As String
Dim strDateFormat As String
Dim strSub As String
'keep the current date format
strDateFormat = DateTime.DateFormat
DateTime.DateFormat = "EEE, d/MMM/yyyy HH:mm:ss"
strCurrentDateTime = DateTime.Date(DateTime.Now)
strSub = GetErrorSubFromStackTrace(strStackTrace)
str = File.ReadString(strAppDir, "AppLog.txt")
If str.Length = 0 Then
File.WriteString(strAppDir, "AppLog.txt", strCurrentDateTime & CRLF & strErrorMsg & " in " & strSub)
Else
File.WriteString(strAppDir, "AppLog.txt", str & CRLF & CRLF & strCurrentDateTime & CRLF & strErrorMsg & " in " & strSub)
End If
DateTime.DateFormat = strDateFormat
End Sub
Sub GetErrorSubFromStackTrace(strStackTrace As String) As String
Dim iPos1 As Int
Dim iPos2 As Int
iPos1 = strStackTrace.IndexOf("b4a.sqlitelight1.") + 17
iPos2 = strStackTrace.IndexOf2("(", iPos1)
Return strStackTrace.SubString2(iPos1, iPos2).Replace("_", "")
End Sub