How to override handleUncaughtException without changing its functionallity

Viewed 712

I asked this question some time back on Stackoverflow, the answer worked for me, It overrides thehandleUncaughtException, I save the exception and throws the default Unfortunately app has stopped working, but when i integrated this in my app, I am facing an issue.

This is the answer i got.

private Thread.UncaughtExceptionHandler defaultExceptionHandler;

    public void registerCrash(){
        defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();

        Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler(){
            @Override
            public void uncaughtException (Thread thread, Throwable e){
                handleUncaughtException (thread, e);
                if(defaultExceptionHandler != null){
                    defaultExceptionHandler.uncaughtException(thread, e);
                }
            }
        });
    } 

What it does, first it goes to handleUncaughtException (thread, e); i save the crash log in this method, then it reads this line

 if(defaultExceptionHandler != null){
    defaultExceptionHandler.uncaughtException(thread, e);
}

here we throw uncaught exception again, so it goes to the first line again, and again saves the exception, and this goes in loop, and application becomes not responding.

What i want is to save crash log, and then show the default Unfortunate message to user.

EDIT

On Application launch it reads this;

defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();

When application crashes, it reads these lines

Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler(){
    @Override
    public void uncaughtException (Thread thread, Throwable e){

        handleUncaughtException (thread, e); //Custom Method 

        if(defaultExceptionHandler != null){
            defaultExceptionHandler.uncaughtException(thread, e);
        }
    }

So it first goes to handleUncaughtException() there i have provided custom implementation, then it goes to this;

if(defaultExceptionHandler != null){
  defaultExceptionHandler.uncaughtException(thread, e);
}

The defaultExceptionHandler is never null; So it goes in a loop in case of multiple crashes.

I have tried adding count there, but it was 0 each time.

2 Answers
Related