(Java)线程在异常捕获时莫名其妙地停止



我被困在一个真正的令人挠头的中间。我的程序到达了一个点,它应该抛出一个异常,并有它的父进程捕获,除了在那里的某个地方,线程似乎只是停止工作,我无法解释为什么。

我的程序很复杂,但这是我问题的本质{

public class ClassOne {
    public CustomClass computeCustomClass() throws IOException {
        //CustomClass is an elsewhere defined valid class in my code.
        try {
            //The core code of this "computeCustomClass" operation has the
            //potential of throwing a "CustomException", an Exception class
            //of my own creation.
        } catch (CustomException e){
            //I have inserted a logging utility here and it is logging that
            //this "catch" process is definitely being executed.
            //I will now wrap the CustomException in an IOException, as the
            //core code of "createCustomClass()" has the potential to generate
            //it's own IOExceptions, and the handling of a CustomException should
            //be done just the same by a parent process as if an IOException had
            //occurred.
            throw new IOException(e);
        }
    }
}
public class ClassTwo {
    private ClassOne myObject;
    public void processData(){
        try{
            //I've inserted a logging code here to track when this line is
            //executed.
            CustomClass data = myObject.computeCustomClass();
            //Another bit of logging code goes here and records when the
            //"computeCustomClass()" request goes off without a hitch.
            // Code goes here that processes the "data" variable;
        } catch (IOException e){
            //There is logging code here, but it NEVER records this "catch"
            //section being executed! Even when the "CustomException" catcher
            //in ClassOne.computeCustomClass() is logged as having executed!
            //It's as if the thread running this code abruptly stops without
            //throwing any exceptions/errors or any indication as to what's
            //occurred!
        }
    }
}

让事情变得更加混乱,我有另一个线程与执行上述代码的线程并发运行。第二个线程的任务之一是发布关于另一个线程的常规日志。即使在发生任何阻止"catch IOException"代码执行的情况后,应该执行它的线程报告"isAlive()"的值为"真",而"isInterrupted()"的值为"假"。

我不知道发生了什么事。有人知道为什么它会在这里停滞吗,或者有人能建议一种诊断故障的方法吗?

我从grepCode中看到,构造一个带有原因的异常实际上调用了原因上的toString()方法。

如果您的自定义异常有一个toString()方法,可能会抛出异常或导致一些显著的时间延迟,那么这可能是您的问题的根源。

你也可以暂时使用:

//} catch (IOException e){
} catch (Throwable e){

,以防你的catch块被绕过

最新更新