Java 捕获错误在循环的第二次迭代中不起作用
Java catch error not working on second iteration of loop
我有一个嵌套的 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
的标准原因包装,您可以检查 TransactionAbortedException
的 getCause()
returns 来弄清楚到底发生了什么,但这似乎有点难看。库不向您抛出根异常可能是有原因的。我建议查看其文档以找出原因。
我有一个嵌套的 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
的标准原因包装,您可以检查 TransactionAbortedException
的 getCause()
returns 来弄清楚到底发生了什么,但这似乎有点难看。库不向您抛出根异常可能是有原因的。我建议查看其文档以找出原因。