G-5060: Avoid unhandled exceptions.

Major

Reliability

Reason

This may be your intention, but you should review the code to confirm this behavior.

If you are raising an error in a program, then you are clearly predicting a situation in which that error will occur. You should consider including a handler in your code for predictable errors, allowing for a graceful and informative failure. After all, it is much more difficult for an enclosing block to be aware of the various errors you might raise and more importantly, what should be done in response to the error.

The form that this failure takes does not necessarily need to be an exception. When writing functions, you may well decide that in the case of certain exceptions, you will want to return a value such as NULL, rather than allow an exception to propagate out of the function.

Example (bad)

CREATE OR REPLACE PACKAGE BODY department_api IS
   FUNCTION name_by_id (in_id IN departments.department_id%TYPE) 
      RETURN departments.department_name%TYPE IS
      l_department_name departments.department_name%TYPE;
   BEGIN
      SELECT department_name
        INTO l_department_name
        FROM departments
       WHERE department_id = in_id;

      RETURN l_department_name;   
   END name_by_id;
END department_api;
/

Example (good)

CREATE OR REPLACE PACKAGE BODY department_api IS
   FUNCTION name_by_id (in_id IN departments.department_id%TYPE) 
      RETURN departments.department_name%TYPE IS
      l_department_name departments.department_name%TYPE;
   BEGIN
      SELECT department_name
        INTO l_department_name
        FROM departments
       WHERE department_id = in_id;

      RETURN l_department_name;  
   EXCEPTION
      WHEN NO_DATA_FOUND THEN RETURN NULL;
      WHEN TOO_MANY_ROWS THEN RAISE;
   END name_by_id;
END department_api;
/