Blog Infos
Author
Published
Topics
Published

To have your app crash in the hands of the users is the ultimate failure for any developer. That naturally raises the question of how do we make our app 100% crash-free. Well, that’s what we will find out today. But before we begin I want to make it clear that we will be simply handling the crash and preventing our app from crashing by no means does that mean our app does not have a bug. No Crashes do not equal No Bugs.

Alright now for the fun part. We will start by creating an Interface to handle our crashes lets call it ExceptionListener.

interface ExceptionListener {
fun uncaughtException(thread: Thread, throwable: Throwable)
}

In our application class, we will implement the ExceptionListener interface and put the code here to handle the crash and indicate to the user that something is wrong.

class ExampleApp : Application(), ExceptionListener {
override fun uncaughtException(thread: Thread, throwable: Throwable) {
// TODO Make sure you are logging this issue some where like Crashlytics.
// Also indicate that something went wrong to the user like maybe a dialog or an activity.
Log.d("ExampleApp", t.message)
}
}
view raw ExampleApp.kt hosted with ❤ by GitHub

Now for the actual code that will catch our error. We will add the below method in the application class and call it in the onCreate of the application class. This method will simply set up a listener and call the uncaughtException method we created earlier.

fun setupExceptionHandler(){
Handler(Looper.getMainLooper()).post {
while (true) {
try {
Looper.loop()
} catch (e: Throwable) {
uncaughtException(Looper.getMainLooper().thread, e)
}
}
}
Thread.setDefaultUncaughtExceptionHandler { t, e ->
uncaughtException(t, e)
}
}

Just in case you are wondering this is the complete application.

class ExampleApp : Application(), ExceptionListener {
override fun onCreate() {
super.onCreate()
setupExceptionHandler()
}
override fun uncaughtException(thread: Thread, throwable: Throwable) {
// TODO Make sure you are logging this issue some where like Crashlytics.
// Also indicate that something went wrong to the user like maybe a dialog or an activity.
Log.d("ExampleApp", t.message)
}
fun setupExceptionHandler(){
Handler(Looper.getMainLooper()).post {
while (true) {
try {
Looper.loop()
} catch (e: Throwable) {
uncaughtException(Looper.getMainLooper().thread, e)
}
}
}
Thread.setDefaultUncaughtExceptionHandler { t, e ->
uncaughtException(t, e)
}
}
}
view raw ExampleApp.kt hosted with ❤ by GitHub

Note: In the implementation of the ExceptionListener.uncaughtException() is where you would put something like firebase in place and log the error so you can handle the errors better in the future version. This is by no means a fix to your crashes. This is simply a gracefully way to handle your errors.

Feel free to drop a comment or reach out to me on LinkedIn or Twitter.

This article was originally published on proandroiddev.com on April 04, 2022

Job Offers

Job Offers


    Engineering Manager – Apps Lifecycle

    Zalando SE
    Berlin
    • Full Time
    apply now

    Senior Kotlin Android Entwickler (m/w/d)

    Deutsche Post IT Services (Berlin) GmbH
    Berlin
    • Full Time
    apply now

    ANDROID DEVELOPER (M/F/D)

    Payback GmbH
    Munich, Germany
    • Full Time
    apply now
Load more listings

OUR VIDEO RECOMMENDATION

, ,

The Evolution of Android Graphics in Android 12/13

Android 12 and 13 both added significant new capabilities to Android platform graphics, including RenderEffect, RuntimeShader, and more. At the same time, RenderScript has been deprecated and we’ve introduced the RenderScript Intrinsics Replacement Toolkit. This…
Watch Video

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Advocate and Fast Talking YouTuber
Google

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Ad ...
Google

The Evolution of Android Graphics in Android 12/13

Daniel Galpin
Android Developer Advocat ...
Google

Jobs

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
In this part of our series on introducing Jetpack Compose into an existing project,…
READ MORE
blog
Nowadays authentication has become common in almost all apps. And many of us know…
READ MORE
blog
Collections are a set of interfaces and classes that implement highly optimised data structures.…
READ MORE

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu