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

Thread.setDefaultUncaughtExceptionHandler(
	new UncaughtExceptionHandler() {

		@Override
		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();
			System.exit(1);
		}
	});

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!


Actions

Information

3 responses

28 05 2015
Anonymous

App closes before toast

11 12 2015
issac

good working fine but it goes the previous activity

26 08 2016
ffeff

ch

Give Your feedback:

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

WordPress.com Logo

You are commenting using your WordPress.com 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: