mucashinto mucashinto - 4 months ago 23
Java Question

Labels in Java for exception handling

I'm doing the Java Associate level certification and while we are expressly told we won't be tested on labels, we have been shown them. Looking on here though the advice seems to be never use labels?

I'd like to use them in a catch block/user input console as a means of validating input.

do {//Keep calculator going as long as user wants
do {
try {//Force user to input a whole number
System.out.print("\nPlease enter the Mark you want to Calculate\n(Enter marks between " + GradeCalculator.getMIN_MARK() + " and " + GradeCalculator.getMAX_MARK() + " only): ");
mark = Integer.parseInt(scn.nextLine());
}catch(NumberFormatException nfe) {
System.out.println("\nInvalid entry - Please ensure entry is a number only.");
continue numInputCheck;
gradeCalc.isValidMark(mark);//Ensure input is within valid range

Is this bad coding?


No this is not actual example because continue can do all the job by itself without the help of the label . A good example is when you have 2 nested loops and you want to break the outer loop from a condition in the inner loop

    outerloop:while(condition1) {

                 while(condition2) {
                           break outerloop;