11 Jan 2024
09:50 PM
- last edited on
24 Jan 2024
08:30 AM
by
Michal_Gebacki
Hi,
In an Android app, I am implementing a custom caught exception handler to add specific behavior when an uncaught exception is encountered instead of allowing the app to crash. However, I still need to report crashes prevnted by the exception handler in Dynatrace for app's metrics. The app currently uses com.dynatrace.tools.android:gradle-plugin:8.273.1.1003 for action and event monitoring.
Currently, when I add the custom exception handler, the app crashes no longer show in Dynatrace Managed -> Applications -> AppName -> Crash Analysis.
I have looked in the Dynatrace APIs, but have not found a way to still report crashes with the custom exception handler, only to report errors. Is there a way to add code to manually report a crash or have a caught exception still show in the crash statistics? Thank you!
Example Code
class UniversalExceptionHandler @Inject constructor(...) : Thread.UncaughtExceptionHandler { override fun uncaughtException(thread: Thread, exception: Throwable) { // custom code for handling uncaught exceptions instead of crashing } }
class MyAndroidApplication : Application(), HasAndroidInjector { override fun onCreate() { super.onCreate()
// set custom exception handler Thread.setDefaultUncaughtExceptionHandler(universalExceptionHandler) }
Solved! Go to Solution.
15 Jan 2024 04:40 PM
Hey rwise25,
Android only supports one Thread.UncaughtExceptionHandler. By using the method Thread.setDefaultUncaughtExceptionHandler, you are removing the crash reporting capability of OneAgent for Android. Crash reporting tools like Dynatrace are preserving the original handler and forwarding the crash information to them to ensure that multiple handlers can be used at the same time. Normally, we would recommend adding the same logic to the custom handler:
override fun uncaughtException(thread: Thread, exception: Throwable) {
// custom logic
// forward the crash to the next Thread.UncaughtExceptionHandler
originalHandler?.uncaughtException(thread, exception)
}
The original handler can be obtained via the method getDefaultUncaughtExceptionHandler before it is replaced with setDefaultUncaughtExceptionHandler.
When you want to replace the default app crash behavior, then you can't use this solution. Because OneAgent will notify the Android OS and the system will terminate the application. In this case, you have to report the uncaught exception as error, because it is now a "caught exception" and the user session continues.
Instead of the crash analysis screen, you can use the multidimensional analysis and error details screen to analyze the reported exceptions.