Try-Catch-Finally, Break, While Loops, If Else and Nested break / continue


Try ... Catch ... Finally

The try { ... } catch ( ... ) { ... } control structure is used for handling Exceptions

String userInput = "xyz";
try {
    int userAge = Integer.parseInt(userInput);
    if (userAge >= 18) {
        System.out.println("You are eligible to vote!");
    } else {
        System.out.println("Sorry, you are not eligible to vote yet.");
    }
} catch (NumberFormatException exception) {
    System.err.println("Invalid input. '" + userInput + "' is not a valid integer.");
}

This would print:

Invalid input. 'xyz' is not a valid integer.

A finally clause can be added after the catch. The finally clause would always be executed, regardless of whether an exception was thrown.

try { ... } catch ( ... ) { ... } finally { ... }

String userInputAge = "xyz";
try {
    int userAge = Integer.parseInt(userInputAge);
    if (userAge >= 21) {
        System.out.println("Congratulations! You are eligible to enter.");
    } else {
        System.out.println("Sorry, you must be at least 21 years old to enter.");
    }
} catch (NumberFormatException e) {
    System.err.println("Invalid input. '" + userInputAge + "' is not a valid integer.");
} finally {
    System.out.println("This code will always be executed, even if an exception occurs.");
}

This would print:

Invalid input. 'xyz' is not a valid integer.
This code will always be executed, even if an exception occurs.

Break

The break statement ends a loop (like for, while) or the evaluation of a switch statement.

Loop:

The way you store properties files as XML files is very similar to the way you would store them as .properties files. Just instead of using the store() you would use storeToXML().

while(true) {
    if(someCondition == 5) {
        break;
    }
}

The loop in the example would run forever. But when someCondition equals 5 at some point of execution, then the loop ends.

If multiple loops are cascaded, only the most inner loop ends using break.


While Loops

int i = 0;
while (i < 100) { // condition gets checked BEFORE the loop body executes
    System.out.println(i);
    i++;
}

A while loop runs as long as the condition inside the parentheses is true. This is also called the "pre-test loop" structure because the conditional statement must be met before the main loop body is performed every time.

The curly braces are optional if the loop contains just one statement, but some coding style conventions prefers having the braces regardless.


If / Else

int i = 2;
if (i < 2) {
    System.out.println("i is less than 2");
} else {
    System.out.println("i is greater than 2");
}

An if statement executes code conditionally depending on the result of the condition in parentheses. When condition in parentheses is true it will enter to the block of if statement which is defined by curly braces like { and }. opening bracket till the closing bracket is the scope of the if statement.

The else block is optional and can be omitted. It runs if the if statement is false and does not run if the if statement is true Because in that case if statement executes.

See also: Ternary If


Nested break / continue

It's possible to break / continue to an outer loop by using label statements:

outerloop:
for(...) {
	innerloop:
	for(...) {
		if(condition1)
			break outerloop;
		if(condition2)
			continue innerloop; // equivalent to: continue;
	}
}

There is no other use for labels in Java.

Basic Programs