SetDefaultUncaughtExceptionHandler makes an application crash silently

CustomExceptionHandler

public class CustomExceptionHandler implements UncaughtExceptionHandler { private Context ctx; private ContentResolver cr; public CustomExceptionHandler(Context ctx, ContentResolver cr) { this.ctx = ctx; this.cr = cr; } public void uncaughtException(Thread t, Throwable e) { final Writer result = new StringWriter(); final PrintWriter printWriter = new PrintWriter(result); e.printStackTrace(printWriter); String stacktrace = result.toString(); printWriter.close(); String deviceUuid = Utilities.DeviceUuid(ctx, cr); String bluetoothName = Utilities.LocalBluetoothName(); AsyncTasks.ErrorLogTask logTask = new AsyncTasks.ErrorLogTask(e, bluetoothName, deviceUuid, stacktrace); logTask.execute(); } } 

Called from my main activity:

 Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler(getBaseContext(), getContentResolver())); 

When an exception occurs, I do not receive a pop-up message "Sorry, stopped." Its just a black screen. If I remove my call from my main action, in other words, no longer use CustomExceptionHandler , I get the default behavior.

Is there a way to implement the default error behavior in my class?

Thanks in advance!

+4
source share
1 answer

At the end of the exception handler, you can add the following: to get the "unfortunately stopped" dialog:

 System.exit(1); 

However, this will end the process, which means your AsyncTask will not complete.

In any case, I would doubt that your code would work reliably in any case if you are in uncaughtExceptionHandler because you do not know what the state of your application is. It may or may not work. What you can also try is to create a new topic in your uncaughtExceptionHandler and a slightly sleeping thread, and then stop the application using System.exit (). This may give your AsyncTask enough time to complete.

+3
source

All Articles