ron
ron

Reputation: 5279

UncaughtExceptionHandler prob

I have a class (in it's own file)

public class UncaughtExceptionHandler implements 

 java.lang.Thread.UncaughtExceptionHandler

In my main program I have the following:-

Thread.setDefaultUncaughtExceptionHandler( new UncaughtExceptionHandler());

When compiling I get the following error:-

cannot instantiate the type Thread.UncaughtExceptionHandler

Am I missing some basic point here?

Here is the code: import java.io.*;

import android.content.*;

public class UncaughtExceptionHandler implements java.lang.Thread.UncaughtExceptionHandler { private final Context myContext;

public UncaughtExceptionHandler(Context context) {
    myContext = context;
}

public void uncaughtException(Thread thread, Throwable exception) {
        boolean crashedOnLastClose = false;
    MyApplication.writeProgressLog("before isfinishing".ERROR_FILE_NAME);
    // The application is being destroyed by the O/S
        crashedOnLastClose = true;

        MyApplication.writeProgressLog("after isfinishing",ERROR_FILE_NAME);


    }   


}

Upvotes: 0

Views: 2255

Answers (3)

gabor
gabor

Reputation: 450

My IDE is having a similar problem with the same construct using @Override. The method uncaughtException(Thread, Throwable) of type new Thread.UncaughtExceptionHandler(){} must override a superclass method:

        Thread.UncaughtExceptionHandler uce = new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                String s = "ssh to user@" + host + " failed";
                log.error(s);
                throw new IllegalStateException(s, e);
            }
        };

Upvotes: 0

EvilDuck
EvilDuck

Reputation: 4436

UncaughtExceptionHandler is an interface, you cannot instantiate an interface. You need to implement its methods.

Your code should look like this:

Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        // your code
    }
});

Hope this helps.

Upvotes: 0

Sherif elKhatib
Sherif elKhatib

Reputation: 45942

ISSUE

In the statement

Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler());

new UncaughtExceptionHandler()

is actually

new java.lang.Thread.UncaughtExceptionHandler()

This is throwing the exception

SOLUTION

Check file UncaughtExceptionHandler.java

Find the package: (on the top of the file)

package your.package;

Now go to your main program

Replace

Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler()); 

with

Thread.setDefaultUncaughtExceptionHandler(new your.package.UncaughtExceptionHandler()); 

Upvotes: 2

Related Questions