Android: handle uncaught exceptions

28 12 2013

To avoid the ‘unfortunately app has stopped‘ dialog box, you can handle all uncaught exceptions in your app by setting custom handler.

Uncaught exception Handler

Paste this code in the beginning of your app. This will setup the handler. Do it only once, for example in onCreate of your fisrt activity that is displayed to the user

	new UncaughtExceptionHandler() {

		public void uncaughtException(Thread thread, Throwable ex) {
			Log.e("Error", "Unhandled exception: " + ex.getMessage());
			Toast.makeText(getApplicationContext(), R.string.app_fatalError, Toast.LENGTH_LONG).show();

Did I help you?
I manage this blog and share my knowledge for free, sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!



3 responses

28 05 2015

App closes before toast

11 12 2015

good working fine but it goes the previous activity

26 08 2016


Give Your feedback:

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: