Java catch错误无法在循环的第二次迭代中工作

时间:2015-05-09 22:46:36

标签: java exception for-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

1 个答案:

答案 0 :(得分:1)

根据您发布的日志输出判断,原因似乎是它实际上并不是DuplicateIdentityException被抛回到您的代码中,而是您所拥有的这个库由于某种原因,使用TransactionAbortedException包裹它。检查堆栈跟踪以查看实际抛出的函数可能有助于您找出发生这种情况的原因。

如果代码使用java.lang.Throwable的标准原因包装,您可以检查TransactionAbortedException&#39; s getCause()返回的内容,以确定实际发生的情况,但这看起来很丑陋。这可能是图书馆没有将根异常抛回给你的原因。我建议查看其文档以找出原因。