简体   繁体   中英

(Java) Thread Inexplicably Stalling at an Exception Catch

I'm stuck in the middle of a real headscratcher here. My program gets to a point where it's supposed to throw an Exception and have it caught by it's parent process, except somewhere in there, the Thread seems to simply stop functioning and I can't explain why.

My program is very complex, but here is the essence of my problem {

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!
        }
    }
}

To make matters all the more confusing, I have another thread that runs concurrently to the one that executes the above code. One of this second thread's jobs is to post regular logs about the other thread. Even after whatever occurs that prevents the "catch IOException" code from executing, the thread that SHOULD be executing it reports a "true" value for "isAlive()", and a "false" value for "isInterrupted()".

I don't know what's happening. Any ideas why it might be stalling here, or can somebody suggest a way of diagnosing what the failure actually is?

I see from grepCode that constructing an exception with a cause actually calls the toString() method on the cause.

If your custom exception has a toString() method that could throw an exception or cause some significant time delay then this could be a source of your problem.

You could also - temporarily - use:

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

just in case you catch block is being bypassed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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