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) | |
} | |
} |
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) | |
} | |
} | |
} |
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