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

时间:2014-05-07 14:39:23

标签: java multithreading exception try-catch

我被困在一个真正的headscratcher中间。我的程序到了一个点,它应该抛出一个Exception并让它被它的父进程捕获,除了那里的某个地方,Thread似乎只是停止运行而我无法解释原因。

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

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()”的“true”值,以及“isInterrupted()”的“false”值。

我不知道发生了什么。任何想法为什么它可能在这里停滞,或者有人可以建议一种方法来诊断失败实际上是什么?

1 个答案:

答案 0 :(得分:1)

我从grepCode看到构造带有原因的异常实际上会在原因上调用toString()方法。

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

你也可以 - 暂时 - 使用:

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

以防万一你被挡住了。

相关问题