Question

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");
 }
}
Was it helpful?

Solution

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.

I'm afraid you are wrong. The point of try-catch is that we execute some code in the try that might fail. If an exception is thrown, then we handle the failure in the catch block, then carry on as normal (rather than propagating the exception to a higher level, possibly exiting the entire program), executing any further code after the catch block.

Code in a finally block is executed regardless of whether an exception is thrown. This is used for closing resources (such as open files) that need to be cleaned up whatever happens.

If you want to skip everything after the exception is thrown, then there is no point handling the exception with try-catch. You would be better off handling the exception at a higher level (though in your example you are already at the top level i.e. main).

OTHER TIPS

When a method throws an exception inside a try block it skips all the other methods in the try block, executes the catch block and then resumes from the next line after the catch block.

To prevent this, add a System.exit(0); to the catch block

If you also have a finally block, it gets executed even if you close the program in the catch block.

Java uses the termination model of exception handling which basically behaves as you experienced it:

further processing in that method is terminated and control is transferred to the nearest exception handler that can handle the type of exception encountered from here: http://www.informit.com/articles/article.aspx?p=464630&seqNum=4.

When the exception handler (catch block) finishes processing the execution continuous at the first line of code after the try/catch block.

In the resumption model of exception handling, after the exception is handled the flow returns at the place which the exception was thrown and the program continuous from there.

Use System.exit(0); within the catch block.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top