(Java)线程在异常捕获时莫名其妙地暂停 我被困在一个真正的划痕器中间。我的程序到了应该抛出异常并被其父进程捕获的地步,除了在那里的某个地方,线程似乎只是停止运行,我无法解释为什么
我的程序非常复杂,但这是我问题的本质{(Java)线程在异常捕获时莫名其妙地暂停 我被困在一个真正的划痕器中间。我的程序到了应该抛出异常并被其父进程捕获的地步,除了在那里的某个地方,线程似乎只是停止运行,我无法解释为什么,java,multithreading,exception,try-catch,Java,Multithreading,Exception,Try Catch,我的程序非常复杂,但这是我问题的本质{ 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"
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”
我不知道发生了什么。知道它为什么会在这里停滞吗,或者有人能建议一种诊断故障的方法吗?我从中看到,构造一个带有原因的异常实际上调用了关于原因的toString()
方法
如果您的自定义异常具有一个toString()
方法,该方法可能引发异常或导致一些显著的时间延迟,那么这可能是您的问题的根源
您还可以临时使用:
//} catch (IOException e){
} catch (Throwable e){
以防万一您的catch块被绕过。看一看。看起来并发情况下的异常处理有点不同。我认为这不是同一个问题。我提到的第二个线程实际上没有运行上面的任何代码,也没有尝试捕获其中生成的任何异常。它只是(就本问题而言)记录执行上述代码的线程上的常量报告,仅此而已。正如我所解释的,这些报告不断声明问题线程仍然处于活动状态,并且没有中断。此外,IOException永远不会到达线程的未捕获异常处理程序,因为它包含在一个try/catch部分中,该部分应包含处理它本身。您是否尝试过扩展类2的processData()中的捕获?可能是由于某种原因引发了另一个异常,并被“catch(IOException e)”忽略了。要让任何东西都无法通过,请使用“catch(Throwable e)”是的,除了IOException捕获之外,我还添加了一个“Throwable”"如果执行,同样应该记录捕获。但从来没有。即使这是某个未捕获异常的事件,我仍然应该在命令提示符中看到生成该异常的报告,就像我对遇到的其他未捕获异常/错误所做的一样。自定义异常的任何部分都会引发异常吗?特别是在运行时例外。也许包装过程调用了它的
toString
,如果它抛出了一个NPE,可能会导致一卡车的悲伤。你是对的。考虑到OP说他试图捕捉所有丢弃的东西,除了在toString()中无限循环或非常长的操作之外,没有其他解释了当然,除了OP的特定JRE中的错误或他报告问题时的错误。这个答案应该被接受。