Home
In labeled loops EXIT should exit the label
Description
The Pl/SQL code rule "In labeled loops EXIT should exit the label" states that when using labeled loops, the EXIT statement should be used to exit the loop with the specified label. This ensures that the loop is exited at the correct point, and that any code after the loop is not executed. This rule helps to ensure that the code is written correctly and that the program runs as expected.
Key Benefits
- Reduces complexity: EXIT exits the label, rather than the loop, which reduces the complexity of the code.
- Improves readability: EXIT helps to make the code more readable, as the label name makes it easier to identify the loop.
- Easier debugging: EXIT helps to make debugging easier, as it is easier to identify the loop and its associated label.
Non-compliant Code Example
DECLARE errorLog_ EXCEPTION;
BEGIN
<<CUSTOMER_TABLE_ITERATION>> --Non compliant code (EXIT is not labeled as loop label)
FOR i IN 1 .. CUSTOMER_TABLE.COUNT
LOOP
IF CUSTOMER_TABLE(i).Id > 25 THEN
EXIT;
ELSIF CUSTOMER_TABLE(i) IS NOT NULL THEN
DBMS_OUTPUT.PUT( i || ' = (' || CUSTOMER_TABLE(i).Name || ', ' || CUSTOMER_TABLE(i).PhoneNumber || ')' );
END IF;
END LOOP;
EXCEPTION
WHEN ERRORS THEN
RETURN;
END CUSTOMER_TABLE_ITERATION;
Compliant Code Example
DECLARE errorLog_ EXCEPTION;
BEGIN
<<CUSTOMER_TABLE_ITERATION>> --Compliant code (EXIT is labeled as loop label)
FOR i IN 1 .. CUSTOMER_TABLE.COUNT
LOOP
IF CUSTOMER_TABLE(i).Id > 25 THEN
EXIT CUSTOMER_TABLE_ITERATION;
ELSIF CUSTOMER_TABLE(i) IS NOT NULL THEN
DBMS_OUTPUT.PUT( i || ' = (' || CUSTOMER_TABLE(i).Name || ', ' || CUSTOMER_TABLE(i).PhoneNumber || ')' );
END IF;
END LOOP;
EXCEPTION
WHEN ERRORS THEN
RETURN;
END CUSTOMER_TABLE_ITERATION;