Home

CATCH clauses should do more than rethrow

Description

    This code rule states that any CATCH clauses used in a SQL Server script should not simply rethrow the exception, but should also do something else, such as logging the error, or performing some other action. This helps to ensure that exceptions are handled properly, and that errors are not simply ignored.

Key Benefits

  • Eliminates redundant code - CATCH clauses should do more than just rethrow the exception, reducing the amount of redundant code.
  • Better debugging - CATCH clauses should provide more meaningful information about the exception, making it easier to debug.
  • More efficient exception handling - CATCH clauses should provide a way to handle the exception more efficiently, instead of just rethrowing it.

 

Non-compliant Code Example

BEGIN TRY  
    EXECUTE usp_ExampleProc;  
END TRY  
BEGIN CATCH  
   THROW; --Non compliant code (In the catch block we simply throwing the exception)
END CATCH;

Compliant Code Example

BEGIN TRY  
    EXECUTE usp_ExampleProc;  
END TRY  
BEGIN CATCH  
    SELECT    --Compliant code (In the catch block we process the exception and throw)
        ERROR_NUMBER() AS ErrorNumber  
        ,ERROR_MESSAGE() AS ErrorMessage;  
    THROW;
END CATCH;
Visual Expert 2024
 VETSQLRULE50