Java provides six language constructs to transfer control or exit loops in a program.
- try …catch …finally
continue …statement exits the current iteration and starts executing the next iteration.
For example, the following code prints all numbers except number 3.
return …statement stops code execution of a method and transfer control back to the calling code.
try …catch …finally …statement
try …catch …finally …statement is used for handling exceptions. (Further, discuss in section ‘Exception Handling’.)
throws …statement is used for handling exceptions. (Further, discuss in section ‘Exception Handling’.)
assert …statements are used to validate the assumptions made about the program. Assertions are expected to be true when assert statements are executed. In case it is false, the Java Virtual Machine (JVM) throws a special error of AssertionError class. Assertion errors are not handled but allowed to propagate to the top level.
Note: Assertion facility can be enabled or disabled at run-time. If disabled, assert statements are not executed during run-time.