gpt4 book ai didi

Java catch 错误在循环的第二次迭代中不起作用

转载 作者:行者123 更新时间:2023-11-30 08:12:45 26 4
gpt4 key购买 nike

我有一个嵌套的 try > catch 情况,其中循环内的内部 catch 正在检查可能的预期异常。所有这些对于循环的第一次迭代都工作得很好。如果发现重复项,则会报告并进入第二轮,如果发现另一个重复项,则抛出外部异常,而不是内部异常。这可能有一个很好/明显的原因,但我和我的研究没有注意到。

非常感谢您的帮助。Java 代码如下所示:

try {
// do some stuff

for(Enumeration e=wholeResult.enumerateProduct();e.hasMoreElements();){
tmpProduct = (Product)e.nextElement();

// do some stuff
try {
db.begin();
db.create(productCategory);
db.commit();
result.addProduct(tmpProduct);

cat.debug("Added " + tmpProduct.toString() + " to " + category.toString());
}
catch (org.exolab.castor.jdo.DuplicateIdentityException err) {
// Enters here first time only
cat.debug("Error caught");
try {
db.rollback();
} catch(TransactionNotInProgressException TnipE) {
}
cat.debug("SKIPPED - " + tmpProduct.toString() + " already in category " + category.toString());
}
}

// do some stuff

}
catch(Exception e) {
// Enters here second time
cat.error("Exception in CategoryAddBulkProductsAction: " + e.toString());
throw e;
}

调试输出/异常:

542  DEBUG [ajp-bio-8009-exec-1] () - Error caught
542 DEBUG [ajp-bio-8009-exec-1] () - SKIPPED - Item with two prices : Item With Two Prices (#99751) already in category Buy Online (#2281)
542 DEBUG [ajp-bio-8009-exec-1] () - Working with Sale Item : My Order Item (#127681)
548 ERROR [ajp-bio-8009-exec-1] () - Exception in CategoryAddBulkProductsAction: org.exolab.castor.jdo.TransactionAbortedException: Nested error: org.exolab.castor.jdo.DuplicateIdentityException: Duplicate identity found for object of type model.objects.ProductCategory with identity <497(497),127681(127681),2281(2281)>: an object with the same identity already exists in persistent storage: Duplicate identity found for object of type model.objects.ProductCategory with identity <497(497),127681(127681),2281(2281)>: an object with the same identity already exists in persistent storage
550 DEBUG [ajp-bio-8009-exec-1] () - Some kind of error occured
550 ERROR [ajp-bio-8009-exec-1] () - org.exolab.castor.jdo.TransactionAbortedException: Nested error: org.exolab.castor.jdo.DuplicateIdentityException: Duplicate identity found for object of type model.objects.ProductCategory with identity <497(497),127681(127681),2281(2281)>: an object with the same identity already exists in persistent storage: Duplicate identity found for object of type model.objects.ProductCategory with identity <497(497),127681(127681),2281(2281)>: an object with the same identity already exists in persistent storage

最佳答案

从您发布的日志输出来看,原因似乎是它实际上并不是被扔回代码的 DuplicateIdentityException ,而是您正在使用的这个库将其包装起来由于某种原因发生 TransactionAbortedException 。检查堆栈跟踪以查看实际抛出的函数可能会帮助您找出发生这种情况的原因。

如果代码使用java.lang.Throwable的标准原因包装,您可以检查TransactionAbortedExceptiongetCause() 返回来弄清楚到底发生了什么,但这看起来有点难看。库不将根异常返回给您可能是有原因的。我建议检查其文档以找出原因。

关于Java catch 错误在循环的第二次迭代中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30145822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com