Newbie
Newbie

Reputation: 146

Unable to catch the exception

I have the following code in Product Implementation

public void updateProduct(Product product) throws ProductNotFoundException {

    Iterator<Product> it = allProducts.iterator();
    Product p = null;
    if (!allProducts.isEmpty()) {
        while (it.hasNext()) {
            p = it.next();
            if (p.getId() == product.getId()) {
                p.setPrice(product.getPrice());
                System.out.println("Successfully updated the product "
                        + product.getId());
            }
        }
    } else {
        System.out.println("No such product " + product.getId());
        throw new ProductNotFoundException(product.getName());
    }
}

// Main

ipod.setId(Integer.MAX_VALUE);

try {
        productDB.updateProduct(ipod);
        Assert.fail("should've gotten ProductNotFoundException");
    } 
catch (ProductNotFoundException pnfe) {
        // expecting this
    }

The Junit is throwing following exception

Exception in thread "main" productdb.util.AssertionFailedError: should've gotten ProductNotFoundException
    at productdb.util.Assert.fail(Assert.java:43)
    **at productdb.ProductDBClient.testProductServer(ProductDBClient.java:85)**
    at productdb.ProductDBClient.main(ProductDBClient.java:20)

Unfortunately, I am unable to use try and catch. Compiler is throwing error saying it cannot be caught.

Can you please provide me some pointers where I am doing incorrectly?

Thanks very much!!

Upvotes: 1

Views: 586

Answers (4)

Bigger
Bigger

Reputation: 1970

The reason is, you are never throwing the exception even if a product is not found among the existent. You only throw the exception when you don't have any product in. Try this code:

public void updateProduct(Product product) throws ProductNotFoundException {

    Iterator<Product> it = allProducts.iterator();
    Product p = null;
    if (!allProducts.isEmpty()) {
        while (it.hasNext()) {
            p = it.next();
            if (p.getId() == product.getId()) {
                p.setPrice(product.getPrice());
                System.out.println("Successfully updated the product "
                        + product.getId());
            }
        }
    }

    if (p == null ) {
        System.out.println("No such product " + product.getId());
        throw new ProductNotFoundException(product.getName());
    }
}

Upvotes: 0

Scorpion
Scorpion

Reputation: 3986

with the code you have, it is difficult to give a definitive answer. The problem is that the else path of the code isn't reached, you should use a debugger to find out why.

however certain tips to solve the problem:

1) change if (p.getId() == product.getId()) { to if (p.getId().equals(product.getId())) { I think your intention is to check for the equality of the id object rather than their reference.

2) step through the debugger to see if the else is reached, alternatively for starters to validate that try-catch is working just comment out the code in if which will give you the confidence that try-catch is working and your code stays in the if path and hence doesn't throw the exception

Upvotes: 0

Jk1
Jk1

Reputation: 11463

You forgot the case when for every available product p.getId() == product.getId() is false. Following the general code snippet logic you should also throw an exception if no id matches the request. Rewrite it like this:

    public void updateProduct(Product product) throws ProductNotFoundException {
    for (Product existing : allProducts) {
        if (existing.getId() == product.getId()) {
            existing.setPrice(product.getPrice());
            return;
        }
    }
    System.out.println("No such product " + product.getId());
    throw new ProductNotFoundException(product.getName());
}

You may also take a look at some functional-style library, like Guava, to ease operations on collections.

Upvotes: 3

RaceBase
RaceBase

Reputation: 18868

I didn't see any reason why it will throw "ProductNotFoundException"

if (!allProducts.isEmpty()) {
        //Code
} else {
        System.out.println("No such product " + product.getId());
        throw new ProductNotFoundException(product.getName());
}

Obviously if your allProducts is not empty, it will never throw the exception

Correct version of your code [Assuming if you are unable to update product, throw exception]

public void updateProduct(Product product) throws ProductNotFoundException {

    Iterator<Product> it = allProducts.iterator();
    Product p = null;
    if (!allProducts.isEmpty()) {
        while (it.hasNext()) {
            p = it.next();
            if (p.getId() == product.getId()) {
                p.setPrice(product.getPrice());
                System.out.println("Successfully updated the product "
                        + product.getId());
                return;
            }
        }
    } 
    throw new ProductNotFoundException(product.getName());
}

Upvotes: 1

Related Questions