cemlo
cemlo

Reputation: 135

Java catch error not working on second iteration of loop

I have a nested try > catch situation where the inner catch inside a loop is checking for a possible expected exception. All this works fine for the first iteration of the loop. If a duplicate is found it is reported and it moves on to the second time round and if another duplicate is found then it throws the outer exception, not the inner one. There is probably a good / obvious reason for this but it escapes me and my research.

Assistance much appreciated. Java code looks like this:

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;
}

Debug output / exception:

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

Upvotes: 1

Views: 153

Answers (1)

Dolda2000
Dolda2000

Reputation: 25855

Judging from the log output you posted, the reason appears to be that it's not actually a DuplicateIdentityException that is being thrown back to your code, but rather that this library that you're using wraps it in a TransactionAbortedException for some reason. Examining the stack trace to see what function actually throws it might help you figure out why this happens.

If the code uses java.lang.Throwable's standard cause wrapping, you could possibly examine what the TransactionAbortedException's getCause() returns to figure out what really happened, but that seems lightly ugly. There's probably a reason why the library doesn't throw the root exception back to you. I'd recommend checking its documentation to figure out why.

Upvotes: 1

Related Questions