1

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
5
  • 1
    Are you sure the exception thrown in the second iteration of the loop is a org.exolab.castor.jdo.DuplicateIdentityException? Commented May 9, 2015 at 22:51
  • @Vincent: sigh I need to go to bed. :-) I only saw the first and third. Thanks again. Commented May 9, 2015 at 23:05
  • @Vincent, here is the full details of the exception thrown second time round: 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: Commented May 9, 2015 at 23:54
  • 1
    @cemlo: Could you please edit that information into the question where it can be properly formatted? However, it looks like the DuplicateIdentityException is wrapped in an outer exception. try/catch clauses don't look at any wrapped exceptions when determining what to catch. Commented May 10, 2015 at 1:58
  • @Dolda2000: done, yes, looks like you might be onto something there. How does a wrapped exception occur? Commented May 10, 2015 at 3:50

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.