Home

EXIT WHEN should be used rather than IF ... THEN EXIT; END IF

Rule description

  • EXIT WHEN should be used rather than IF ... THEN EXIT; END IF

 

Non-compliant Code Example

DECLARE errorLog_ EXCEPTION; 
BEGIN
	FOR i IN 1 .. CUSTOMER_TABLE.COUNT  --Non compliant code (EXIT is used within loop)
		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
	FOR i IN 1 .. CUSTOMER_TABLE.COUNT  --Compliant code (EXIT When is used within loop)
		LOOP
			EXIT WHEN CUSTOMER_TABLE(i).Id > 25; 
				
			IF 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;
Visual Expert 2020
 VEPLSQLRULE82