public static String getElementXpath(DOMElement elt){
String path = "";
for (; elt != null; elt = (DOMElement) elt.getParentNode()){
System.out.println("THIS ONE " + path);
}
System.out.println("NEXT ONE " + path);
return path;
}
下一个甚至不打印。我尝试在 for 循环后添加任何内容,但它们似乎没有运行。难道不应该有我可以捕获的异常吗?我执行函数 getElementXpath 的主要方法抛出异常。我尝试删除抛出异常并得到相同的结果。
最佳答案
在其他地方吞掉异常似乎是唯一可能的解释。将循环封装在 try-catch block 中,打印异常并重新抛出。为了进行测试,捕获 Throwable。也许还可以添加一个finally block 。
My main method which executes the function getElementXpath throws Exception.
这没关系,可能会出现RuntimeException甚至Error。您不应该无缘无故地声明抛出异常
,但它不可能是这里的罪魁祸首。
关于Java:for循环后代码未运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5399533/