How do I return to a previous step after throwing an exception rather than from the beginning of my program? : The code I'm working on is a main method to test a class called Triangle that takes two doubles and is used to calculate the hypotenuse of a triangle.

My code is set up to throw exceptions whenever the inputs of the doubles are negative, not a number or zero. However, whenever an exception is thrown it prompts me to input the command again which is not desired. Once the command is entered it should prompt the user to input a value for the length of the side, and if the input double doesn't meet the criteria then it should ask the user to input a valid double for the length of the side.

Thanks ahead for the help!

My code is as follows:

import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Double sideA = null;
		Double sideB = null;
		Scanner scanner = new Scanner(System.in);
		
		
		while (true) {

			System.out.println("Enter command: ");
			String command = scanner.nextLine();

			try {	// checks command, throws exception if not A, B, C or Q
				if (command.equals("A")) {
					System.out.println("Enter a value for side A:");
					sideA = scanner.nextDouble();						// throws InputMismatchException if not a double
					scanner.nextLine();
					try {
						if (sideA < 0 || sideA == 0.0) {
							throw new InputMismatchException();
						}
					} catch (InputMismatchException e) {
						System.out.println("Value is invalid. Must be positve and a double.");
					}

				}
				else if (command.equals("B")) {
					System.out.println("Enter a value for side B:");
					sideB = scanner.nextDouble();						// throws InputMismatchException if not a double
					scanner.nextLine();

					try {
						if (sideB < 0 || sideB == 0.0) {
							throw new InputMismatchException();
						}
					} catch (InputMismatchException e) {
						System.out.println("Value is invalid. Must be positive and a double.");
					}

				}
				else if (command.equals("C")) {
					Triangle calculate = new Triangle(sideA, sideB);
					System.out.println(calculate.calcHypotenuse());
					break;
				}
				else if (command.equals("Q")) {
					System.exit(0);
				}
				else {
					throw new IllegalArgumentException();
				}

			} 
			catch (IllegalArgumentException e) {
				System.out.println("Command is invalid, Enter A, B, C, or Q.");
			}
			catch (InputMismatchException e) {
				System.out.println("Value is invalid. Must be positve and a double.");
				scanner.nextLine();
			}
			
		}
	}	

}
