PL/SQL & SQL CODING GUIDELINES – PART 7 Larry Nung
AGENDA Exception Handling Reference Q&A 2
EXCEPTION HANDLING
51. NEVER HANDLE UNNAMED EXCEPTIONS USING THE ERROR NUMBER.
BAD BEGIN ... EXCEPTION WHEN OTHERS THEN IF SQLCODE = -2291 THEN ... END IF; END;
GOOD DECLARE e_parent_missing EXCEPTION; PRAGMA EXCEPTION_INIT(e_parent_missing,- 2291); ... BEGIN ... EXCEPTION WHEN e_parent_missing THEN ... END;
52. NEVER ASSIGN PREDEFINED EXCEPTION NAMES TO USER DEFINED EXCEPTIONS.
BAD DECLARE no_data_found EXCEPTION; … BEGIN ... EXCEPTION WHEN no_data_found THEN sys.dbms_output.put_line(co_no_data_found); END;
GOOD DECLARE empty_value EXCEPTION; ... BEGIN ... EXCEPTION WHEN empty_value THEN sys.dbms_output.put_line(co_empty_value); WHEN no_data_found THEN sys.dbms_output.put_line(co_no_data_found); END;
53. AVOID USE OF WHEN OTHERS CLAUSE IN AN EXCEPTION SECTION WITHOUT ANY OTHER SPECIFIC HANDLERS.
BAD EXCEPTION WHEN OTHERS THEN IF SQLCODE = -1 THEN update_instead (...); ELSE err.log; RAISE; END IF;
GOOD EXCEPTION WHEN DUP_VAL_ON_INDEX THEN update_instead (...); WHEN OTHERS THEN err.log; RAISE;
54. AVOID USE OF EXCEPTION_INIT PRAGMA FOR A -20,NNN ERROR
BAD CREATE OR REPLACE PROCEDURE check_hiredate (date_in IN DATE) IS BEGIN IF date_in < ADD_MONTHS (SYSDATE, -1 * 12 * 18) THEN RAISE_APPLICATION_ERROR ( -20734, 'Employee must be 18 years old.'); END IF; END check_hiredate;
GOOD CREATE OR REPLACE PROCEDURE check_hiredate (date_in IN DATE) IS BEGIN IF emp_rules.emp_too_young (date_in) THEN err.raise (errnums.emp_too_young); END IF; END check_hiredate;
55. AVOID USE OF THE RAISE_APPLICATION_ERROR BUILT-IN PROCEDURE WITH A HARD-CODED - 20,NNN ERROR NUMBER OR HARD-CODED MESSAGE.
BAD BEGIN raise_application_error(-20501,'Invalid employee_id'); END;
GOOD BEGIN err_up.raise(in_error => err.co_invalid_employee_id); END;
56. AVOID UNHANDLED EXCEPTIONS
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;
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;
57. AVOID USING ORACLE’S PREDEFINED EXCEPTIONS
BAD BEGIN RAISE NO_DATA_FOUND; END;
GOOD DECLARE my_exception EXCEPTION; BEGIN RAISE my_exception; END;
REFERENCE 25
REFERENCE  Trivadis PL/SQL & SQL Coding Guidelines Version 2.0  http://www.trivadis.com/sites/default/files/downloads/PL SQL_and_SQL_Coding_Guidelines_2_0_HiRes.pdf 26
Q&A 27
QUESTION & ANSWER 28

PL/SQL & SQL CODING GUIDELINES – Part 7