I'm having some issues with my code below. In this program, I input the description, units, and price of an item. I made two custom exception classes for the possibility of the user inputting a negative number. When I run the program, the catch statements do work, but what I find weird is that the statements after the catch block still execute as well. Correct me if I'm wrong, but isn't the program not supposed to execute statements after the catch statement if there is an exception? I was under the impression that the only way to do that was to use a finally block. Any help would be greatly appreciated!
import java.util.Scanner;
public class RetailItemDemo
{
public static void main (String[] args)
{
Scanner keyboard = new Scanner(System.in);
RetailItem item = new RetailItem();
System.out.print("Item description: ");
item.setDescription(keyboard.nextLine());
try
{
System.out.print("Units on hand: ");
item.setUnitsOnHand(keyboard.nextInt());
System.out.print("Item price: ");
item.setPrice(keyboard.nextDouble());
}
catch (NegativeUnitsException nue)
{
System.out.println(nue);
}
catch (NegativePriceException npe)
{
System.out.println(npe);
}
System.out.println("\nThe item is a " + item.getDescription());
System.out.println("There are " + item.getUnitsOnHand() + " units on hand");
System.out.println("The item price is " + item.getPrice() + "\n");
}
}