问题描述:

In Android, but could be in any Java program, i spawn a child thread which throws a RunTimeException. The code looks like this:

 new Thread(new Runnable() {

@Override

public void run() {

throw new RuntimeException();

}

}).start();

This completely terminates my application with FatalException error. I thought that only the child thread would end but the main/parent thread can continue ? is this assumption not correct ?

网友答案:

RuntimeException will usually only kill the thread, but can kill the program. Other things are usually the determinant of this. Would need to see more code, but it may be an issue with androids exception handling in threads.

网友答案:

Android only having main (single) thread.

网友答案:

Pretty sure you can create your own exceptions and do a stopSelf() on the child thread. Not sure exactly what you're trying to do or I'd give an example.

Runnable r = new Runnable()
{
    public void run() {
    while (/*whatever you want to determine end of thread*/)
    {
        try { // this stuff
        } catch (Exception e) { // exception handling
        }
    }
    stopSelf();
    }
};

//Thread start code

this should work and not crash your app...

相关阅读:
Top