suba
suba

Reputation: 33

Custom exception class for java

I need to write a custom exception class for a Java program. For example, when trying to remove an existing product from a list, an exception should be thrown with a statement saying "error". I tried using this, but I don't know if it's right.

public class ProductException extends RuntimeException 
{
    public ProductException(String message) 
    {
        super(message);
    }
}

public class ShoppingCart 
{
    public void removeFromCart(Product p) throws ProductException 
    { 
        int i = cart.indexOf(p);

        if (i >= 0) 
        {
            cart.remove(p);
        } 
        else 
        {
            ProductException e = new ProductException("Error..Product not found");
        }
    }
}

What is the error here and how to correct it? Thanks.

Upvotes: 0

Views: 2069

Answers (3)

cognophile
cognophile

Reputation: 917

You've created a reference to an exception object, but you've not done anything with it. What you need to do is throw the Exception.

As you appear to know, where you create your ShoppingCart object and populate it with Product objects, you can call removeFromCart(...) on that cart object to perform the desired action. A basic example of your calling code being:

ShoppingCart cart = new ShoppingCart();
Product apple = new Product();

cart.addToCart(apple);
cart.removeFromCart(apple);

Here, we create the object and do something with it or upon it. In your example code, the problem is you're not doing anything with the object you created so it immediately goes out of scope and is marked for garbage collection.

Exceptions differ slightly from other objects in that you don't have to create a reference object to use it (as with your ShoppingCart above). What you're trying to do is create the Exception, but we need to throw and catch the Exception as shown below which will implicitly create it for us:

public class ProductException extends RuntimeException 
{
    public ProductException(String message) 
    {
        super(message);
    }
}

public class ShoppingCart 
{
    public void removeFromCart(Product p) throws ProductException 
    { 
        int i = cart.indexOf(p);

        if (i >= 0) {
            cart.remove(p);
        } else {
            throw new ProductException("Error..Product not found");
        }
    }
}

The exception we just raised now needs to be caught in the scope where removeFromCart(...) is called. For example:

public static void main(String[] args) 
{
    ShoppingCart cart = new ShoppingCart();
    Product orange = new Product();

    cart.addToCart(orange);

    try {
        cart.removeFromCart(orange);
    } catch (ProductException ex) { 
        /* 
           Do something... For example, displaying useful information via methods such 
           as ex.getMessage() and ex.getStackTrace() to the user, or Logging the error. 
         */
    } catch (Exception ex) { 
        // Do something...
    }
} 

If you're still unsure or need more content, I'd suggest starting with the Java 'How to Throw Exceptions' tutorial on the Oracle Docs pages where you can learn more about Exceptions and the processes of throwing and catching them, and the associated try, catch, and finally blocks.

Upvotes: 1

Tal Avissar
Tal Avissar

Reputation: 10304

This is the implementation recommended for throwing an exception:

public class ShoppingCart 
{
    public void removeFromCart(Product p) throws ProductException 
    { 
        if (!cart.contains(p)) 
        {
            throw new ProductException("Error..Product not found");
        } 

        cart.remove(p);

    }
}

Upvotes: 0

Rishav Agarwal
Rishav Agarwal

Reputation: 96

You need to throw the exception in your else block:

ProductException e=new ProductException("Error..Product not found");
throw e;  

Now you can handle this exception in your calling function.

Upvotes: 0

Related Questions