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:

hibrid0

Active Member
Licensed User
Longtime User
I test it on my small project and is too useful!!!
Thanks Erel.
Anyware software is a great developer team.
 
Last edited:

PhilipBrown

Active Member
Licensed User
Longtime User
Can anyone verify that the following statement is correct for version 5.5 of B4A?

If a runtime error occurs outside a Try-Catch block, what the user sees will depend on how you have distributed the app and whether the error is caught internally.
If you distribute via Google Play and your application raises an error which is not caught internally, the user will be asked to send a crash report. This happens automatically. If the user agrees to send the report, you can see the result in Google Play Developer Console.
If you distribute your application directly with an apk file and your application raises an error which is not caught internally, the user will see an error crash report as described below.
The same error report is shown when your application raises an error which is outside a Try-Catch block but is nevertheless caught internally, no matter how you have distributed it.
The user sees an error crash report which asks if they wish to continue.​
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
If you haven't added the application_error sub to the starter service then the behavior will be exactly as in previous versions (which means that in most cases the user will see the "Continue? yes no" dialog).

This new feature allows you to handle the crash yourself.
If you return True from Application_Error then the default crash handler will be executed.
In the case of a Google Play app then the user will be asked to send the crash report.

How you handle the crash is up to you. It will not show anything "special" unless you implement it, as in this example.
 

Tan

Member
Licensed User
Longtime User
Hi,

is it in any way possible to show a dialog in "application_error" (e.g. a msgbox... ask the user, if he want's to send a crash report via email)?

If I try that, I get the following exception:
"android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"

Even if I return False, the Activity seems (in that moment) to be in the process of finishing!?! Any other way of showing a dialog?

Best regards
Tan
 

Blue.Sky

Active Member
Licensed User
Longtime User
Hi Erel.
This property in b4a is good
I download your sample and run it
When i press button,app force stop and show email intent
Can i prevent stop app with this property?
 

Blue.Sky

Active Member
Licensed User
Longtime User
Hi Erel.
This property in b4a is good
I download your sample and run it
When i press button,app force stop and show email intent
Can i prevent stop app with this property?
It solved.return false in Application_Error for prevent close or stop app
 

Douglas Farias

Expert
Licensed User
Longtime User
@Erel why you dont add the sub of error on the Main activity already?
why need a service?
B4X:
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
   Return True
End Sub

and the starter service i need to execute this or its automatic?
B4X:
StartService(Starter)
on activity create?
 
Last edited:

Douglas Farias

Expert
Licensed User
Longtime User
Nice, and its possible send the email automatic? or only with user click?
 

MMORETTI964

Member
Licensed User
Longtime User
I'm trying to use this exception to handle errors occurred (in a big project) when Android choose to free memory (and your program was in background for a long time).
When you resume the app sometimes (depends on the memory, AFAIK) your application start with errors (due to images or control not more existant).
As I seen by some heavy-heavy programs (Ex: Magic 2015), when you restore from background the programs (sometimes) magically "restart".
I think there is some systems to trap these kind of "untrappable" errors and restart programmatically the application.
Do you think the approach to these kind of error with the uncaught-excpetions is correct?
Can I restart the application without any kind of system message like "... is terminated" (I should substitute with a message of my own) when I have an exception uncaughted?
I should use a newly created activity to make that (Exit application, message and restart)?
Thank you in advance.
Maurizio
 

MMORETTI964

Member
Licensed User
Longtime User
This is a bug in your code. Please start a new thread for this question for further discussion.
I will, but just for knowing...
Can I restart the application without any kind of system message (I should substitute with a message of my own) when I have an exception uncaughted?
I should use a newly created activity to make that (Exit application, message and restart)?
 

MMORETTI964

Member
Licensed User
Longtime User
You can use StartServiceAt to schedule your app to start and then call ExitApplication to kill the process (not that I recommend this approach).

Can I use ExitApplication inside the Application_Error sub?
 

luke2012

Well-Known Member
Licensed User
Longtime User
Hi @Erel.
I have to implement "Uncaught Exceptions" within an old project that doesn't have the Starter Service.

I have a question about this: Can I implement the Starter Service (mandatory for the "Uncaught Exceptions" implementation) within my old project without impact the previous (core) App code and the main App behavior ?
 
Top