Android Tutorial Uncaught Exceptions

By default, when there is an uncaught exception the program will show a message box with the error title and the user will be asked whether they want to continue or not.

There are several problems with this behavior:
1. The user cannot really know whether the program can continue correctly after the uncaught exception.
2. A crash report will not be sent to Google Play as the error was eventually caught internally.
3. It is inconsistent as the dialog only appears when the error happens with an activity context.
4. It was not possible to override this behavior and allow sending the error with an email for example.

Starting from B4A v5.50 it is possible to change this behavior.
If there is a sub named Application_Error in the Starter service module then the default error dialog will never appear.
Note that the starter service template includes this sub. It is recommended to include this sub in all projects.

B4X:
'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
   Return True
End Sub

If you return True from this sub then the OS default exceptions handler is called. The result is that the app will crash and the crash report will be sent to Google Play (if the user allows it).
This is a good and standard behavior.

If you return False then the default exceptions handler will not be called. This means that the app will continue to run.

A proper usage of this is to allow you to use alternative methods to send the crash report. For example you can use HttpUtils2 to send the StackTrace to your server and then kill the process in the JobDone event.

Notes

- Application_Error will only be called in Release mode. In Debug mode the program will print the error message in the logs and will end.
- Errors that happen when the app is started, before the Starter service is ready will not be trapped. The OS default exceptions handler will handle those errors.
- The starter service must be running for this sub to be raised. It will be running unless you explicitly stop it.

An example of catching the recent logs and sending them with an intent is attached.

 

Attachments

  • SendEmailWithCrashLogs.zip
    7.8 KB · Views: 1,999
Last edited:

EvgenyB4A

Active Member
Licensed User
Longtime User
Hi
I want to send an error by HTTP but the server doesn't get anyting
The code is below:

Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
'wait for 500ms to allow the logs to be updated.
Dim jo As JavaObject
Dim l As Long = 500
jo.InitializeStatic("java.lang.Thread").RunMethod("sleep", Array(l))
logcat.LogCatStop
logs.Append(StackTrace)

' Dim email As Email
' email.To.Add(emailAddress)
' email.Subject = "Program crashed"
' email.Body = logs
' StartActivity(email.GetIntent)

Log("EEERRor " & Main.LinkToServer & " " & Main.DeviceID)

Dim job1 As HttpJob

job1.Initialize("RuntErr", Me)

job1.PostString(Main.LinkToServer & "/" ,"ID=" & Main.DeviceID & "&case=RunTimeError" & "&Details=" & logs)

job1.GetRequest.Timeout = 32000
ToastMessageShow(logs,True)

Return True
End Sub

Sub Service_Destroy

End Sub

Sub JobDone (Job As HttpJob)
Log("AAA")
ExitApplication
End Sub
 

JakeBullet70

Well-Known Member
Licensed User
Longtime User
I was doing something close to what you are doing and had issues. What I ended up doing was writing a file out and on the next run of the program check for the file and send it then. That seems to work much better.
 

peacemaker

Expert
Licensed User
Longtime User
Is it possible to have timestamps in the system log lines ?
 

peacemaker

Expert
Licensed User
Longtime User
Yes, it's about Application_Error, just more detailed log info - is possible ?
 

peacemaker

Expert
Licensed User
Longtime User
Just add into logs.Append(StackTrace) some timestamps
 

Alpandino

Member
Licensed User
Hi,
in my Starter Module I have Application_Error Sub, with Return True, but when my app crashes (every now and then) the user can't see the message asking permission of the user to send a report to Developer Console. The user can see only a message saying: "The app stops unexpectedly" or similar.
I need this kind of report.
Any help or suggestion?
 

FrankBerra

Active Member
Licensed User
Longtime User
I am using the following code in the soubroutine:
B4X:
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Dim jo As JavaObject
    Dim l As Long = 500
    jo.InitializeStatic("java.lang.Thread").RunMethod("sleep", Array(l))
    logcat.LogCatStop
    logs.Append(StackTrace)
    File.WriteString(File.DirDefaultExternal, "CrashReport.txt", logs)
    File.WriteString(File.DirDefaultExternal, "CrashReport.txt", Chr(10) & Chr(10) & "============================" & Chr(10) & Chr(10))
    Return True
End Sub

When something wrong happens the file CrashReport.txt contains just the line "============================".
Where i am wrong? How can i log something more useful?
 

FrankBerra

Active Member
Licensed User
Longtime User
Yes i see the logs in a new email, but i can't save them in a local file.

For example i am using the following code:

B4X:
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean 'Istruzioni prese da qua: https://www.b4x.com/android/forum/threads/uncaught-exceptions.59805/
        'wait for 500ms to allow the logs to be updated.
    Dim jo As JavaObject
    Dim l As Long = 500
    jo.InitializeStatic("java.lang.Thread").RunMethod("sleep", Array(l)) 
    logcat.LogCatStop
    logs.Append(StackTrace)
    File.WriteString(File.DirDefaultExternal, "CrashReport.txt", logs.ToString)
    File.WriteString(File.DirDefaultExternal, "CrashReport.txt", Chr(10) & Chr(10) & "=============" & Chr(10) & Chr(10))
   
    Dim email As Email
    email.To.Add("MyEmail@aaa.com")
    email.Subject = "Program crashed"
    email.Body = logs.ToString
    StartActivity(email.GetIntent)
   
    Return True
End Sub

The email body contains the correct information while the file CrashReport.txt contains just "============="

Where i am wrong?
 
Top