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


    Senior Android Software Engineer (f/m/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now

    Android Test Automation Engineer

    Komoot
    Remote
    • Full Time
    apply now

    Android Software Engineer (f/m/d)

    Paradox Cat GmbH
    Munich
    • Full Time
    apply now

OUR VIDEO RECOMMENDATION

, ,

From Scoped Storage to Photo Picker: Everything to know about Storage

Persistence is a core element of every mobile app. Android provides different APIs to access or expose files with different tradeoffs.
Watch Video

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
Android developer advocate
Google

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
Android developer ad ...
Google

From Scoped Storage to Photo Picker: Everything to know about Storage

Yacine Rezgui
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
This is the second article in an article series that will discuss the dependency…
READ MORE
blog
Let’s suppose that for some reason we are interested in doing some tests with…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Menu