Home

EXCEPTION WHEN ... THEN clauses should do more than RAISE

Description

  • The EXCEPTION WHEN ... THEN clauses should do more than just RAISE an exception. These clauses should also include additional logic to handle the exception, such as logging the error, rolling back the transaction, or performing other corrective actions. This helps to ensure that the application is robust and can handle unexpected errors.

Key Benefits

  • Error Handling: EXCEPTION WHEN ... THEN clauses provide a way to handle errors that occur during the execution of a program. This allows for better control over the flow of the program and can help prevent errors from propagating further.
  • Flexibility: EXCEPTION WHEN ... THEN clauses can be used to customize the behavior of the program based on the type of error that occurs. This allows for more flexibility in how the program is written and can help make it more robust.
  • Debugging: EXCEPTION WHEN ... THEN clauses can help make debugging easier by providing more information about the error that occurred and the context in which it occurred.

 

Non-compliant Code Example

BEGIN
	FOR i IN 1 .. CUSTOMER_TABLE.COUNT
		LOOP
			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 compliant code (Only RAISE when exception occur)
	RAISE;         
END CUSTOMER_TABLE_ITERATION;

Compliant Code Example

BEGIN
	FOR i IN 1 .. CUSTOMER_TABLE.COUNT
		LOOP
			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      --Compliant code (Logging error and then raising error)
	Log_Errors ( 'Error Log...' || Chr(10) ||
		DBMS_UTILITY.FORMAT_ERROR_STACK() );
    Log_Errors ( 'Error Log...' || Chr(10) ||
		DBMS_UTILITY.FORMAT_ERROR_BACKTRACE() );
    DBMS_OUTPUT.PUT_LINE ( '----------' );
    RAISE;
END CUSTOMER_TABLE_ITERATION;
Visual Expert 2024
 VEPLSQLRULE111