简体   繁体   English

如何处理线程中的异常?

[英]How do I handle exceptions in a thread?

I'm working on an Android App. 我正在开发一个Android应用程序。 I have a thread with this structure: 我有一个具有这种结构的线程:

code...
try
    code...
catch
    printexception
code...
try
    code...
catch
    printexception
code..

When I encounter one of these exceptions, I know that the remainder of my code will not work and should not be executed. 当我遇到这些异常之一时,我知道我代码的其余部分将不起作用,因此不应执行。 How I can stop the thread when I encounter an exception? 遇到异常时如何停止线程? Do I have to create a "big try" with all of the code inside? 我是否必须使用所有内部代码来创建“大尝试”? I've been working on code like this for some time, but I have never had to care about exceptions before. 我已经在这样的代码上工作了一段时间了,但是之前我从来不需要关心异常。 What is a good, clean way to accomplish this? 有什么好的,干净的方法可以做到这一点?

this is very simple, try this : 这非常简单,请尝试以下操作:

public static void main(String[] args) {
    Thread thread = new Thread(new RunnableTest());
    thread.start();
}

static class RunnableTest implements Runnable {

    @Override
    public void run() {
        int i;
        try {
            i = 1 / 0;
        } catch (Throwable t) {
            t.printStackTrace();
            return;
        }
        System.out.println(i);
    }

}

for better control over the threads you may use Executors and ThreadPoolExecutors from java.util.Concurreent package! 为了更好地控制线程,您可以使用java.util.Concurreent包中的ExecutorsThreadPoolExecutors

Generally, whenever you are trying to code, you should keep them in try and then under catch get the result. 通常,每当您尝试进行编码时,都应使其处于尝试状态,然后捕捉结果。

its like: 就像是:

try
{
int x = 5/0;
}
catch(Exception e)
{
e.printStackTrace();
}

By mentioning printStackTrace you are able to get the line number and what description of exception. 通过提及printStackTrace,您可以获得行号和异常的描述。 I hope i made my point clear. 我希望我说清楚。

If the code you posted lives within your run() method, you can simply return early whenever an exception occurs, causing the thread to complete and avoiding execution any code that follows. 如果发布的代码位于run()方法中,则只要发生异常,就可以提早返回,从而导致线程完成并避免执行随后的任何代码。

code...
try {
    code...
} catch (Exception e) {
    e.printStackTrace();
    return;
}
code...

You could also set a flag whenever an exception occurs, and periodically check that flag to determine if your thread should continue running. 您还可以在发生异常时设置标志,并定期检查该标志以确定线程是否应继续运行。

bool isCanceled = false;

void run() {
    while (!isCanceled) {
        doSomeWork();
    }
}

void doSomeWork() {
    try {
        code...
    } catch (Exception e) {
        e.printStackTrace();
        isCanceled = true;
        return;
    }
}

Of course you can also structure your code so that any statements that rely on the statements above it are also within the try block. 当然,您也可以对代码进行结构化,以便所有依赖于其上面的语句的语句也都在try块内。 If an exception occurs, flow of control will transfer to the catch block, and any statements within the try block that follow the offending line won't be executed. 如果发生异常,则控制流将转移到catch块,并且try块中紧随其后的行的任何语句都不会执行。 I assume this is what you mean by a "big try" with all the code inside. 我认为这是您对所有代码进行“大尝试”的意思。

code...
try
    code...
    try          // this try won't be executed if the line above it throws an exception
        code...
    catch {
        printexception
        return
    }
    code..
catch {
    printexception
    return            // return early
}
code....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM