Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-2458

"EXCEPTION_INIT -20,NNN" calls should be centralized

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Move this exception declaration to a specialized package.
    • Default Severity:
      Critical
    • Impact:
      High
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Legacy Key:
      CustomExceptionInitCheck
    • Covered Languages:
      PL/SQL
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      30min

      Description

      Centralizing the definitions of custom exceptions comes with two major benefits:

      • The duplication of the exceptions declarations and PRAGMA EXCEPTION_INIT is avoided
      • The risk of associating multiple different exceptions to the same number is reduced

      Noncompliant Code Example

      SET SERVEROUTPUT ON
      
      DECLARE
        user_not_found EXCEPTION;
        PRAGMA EXCEPTION_INIT(user_not_found, -20000); -- Noncompliant, user_not_found is bound to -20000
      BEGIN
        NULL;
      END;
      /
      
      DECLARE
        user_not_found EXCEPTION;
        PRAGMA EXCEPTION_INIT(user_not_found, -20000); -- Noncompliant, user_not_found is again bound to -20000, duplication
      BEGIN
        NULL;
      END;
      /
      
      DECLARE
        wrong_password EXCEPTION;
        PRAGMA EXCEPTION_INIT(wrong_password, -20000); -- Noncompliant, wrong_password is bound to -20000, conflicting with user_not_found
      BEGIN
        NULL;
      END;
      /
      

      Compliant Solution

      SET SERVEROUTPUT ON
      
      CREATE PACKAGE exceptions AS
       user_not_found EXCEPTION;
       wrong_password EXCEPTION;
      
       PRAGMA EXCEPTION_INIT(user_not_found, -20000); -- Non-Compliant (flag as false-positive)
       PRAGMA EXCEPTION_INIT(wrong_password, -20001); -- Non-Compliant (flag as false-positive), conflicts are easier to avoid
      END;
      /
      
      DROP PACKAGE exceptions;
      

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            ann.campbell.2 Ann Campbell
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: