RunDown BassMan

RunDown BassMan

  • NA
  • 22
  • 28.8k

Throwing exceptions linkedList

May 3 2013 10:32 AM
Hey, im creating a exception class to use on my addItem() method to add items to a list
Im getting a error when im throwing the expeption - Unreported exception
Here is my code -

public void addItem(StockItem aItem) throws Exceptions
    {
        Iterator<StockItem> addItemIterator = itemList.iterator();
        boolean addAnother = true;
        while (addAnother == true)
        {
                System.out.println("Add item Interface");
                System.out.print("Enter ItemID\t :>");
                String itemId = sc.next();
                System.out.print("Enter Description\t :>");
                String description = sc.next();
                System.out.print("Enter price\t :>");
                double price = sc.nextDouble();
                System.out.print("Enter Quantity\t :>");
                int quantity = sc.nextInt();
                System.out.print("Enter re-order level\t :>");
                int reOrderLevel = sc.nextInt();
                StockItem item1 = new StockItem(itemId, description, price, quantity, reOrderLevel);
                System.out.println(item1);
                while (addItemIterator.hasNext())
                {
                    if (addItemIterator.next().getItemID().equals(itemId))
                    {
                       Exceptions a = new Exceptions();
                       throw a.AddMethodException("Item already exists");
                    }
                }
                itemList.add(item1);
                System.out.print("Add another (Y/N)\t :>");
                String add = sc.next();
                if ("n".equals(add))
                {
                    addAnother = false;
                }
            }
    }

Here is my exceptions class

public class Exceptions extends Exception

{
    public Exceptions()
    {

    }
   
    public Exception AddMethodException(String message)
    {
        System.out.println(message);
        return null;
    }

    public Exception DeleteMethodException(String message)
    {
        System.out.println(message);
        return null;
    }

}