Home

WHEN OTHERS clauses should be used for exception handling

Rule description

  • WHEN OTHERS clauses should be used for exception handling

 

Non-compliant Code Example

BEGIN
	FOR i IN 1 .. CUSTOMER_TABLE.COUNT  
		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          --Non complaint code (WHEN OTHERS clauses is not used for exception handling)
	    error_message := SQLERRORMSG;
        DBMS_OUTPUT.PUT_LINE (error_message);	    
        RETURN;
END CUSTOMER_TABLE_ITERATION;

Compliant Code Example

BEGIN
	FOR i IN 1 .. CUSTOMER_TABLE.COUNT  
		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          
        error_message := SQLERRORMSG;
        DBMS_OUTPUT.PUT_LINE (error_message);	    
        RETURN;
    WHEN OTHERS THEN          --Complaint code (WHEN OTHERS clauses is used for exception handling)
        error_message := SQLERRORMSG;
        DBMS_OUTPUT.PUT_LINE (error_message);		   
        RETURN;
END CUSTOMER_TABLE_ITERATION;
Visual Expert 2020
 VEPLSQLRULE146