Error Management in Oracle PL/SQL
Error Management in Oracle PL/SQL
Error Management in Oracle PL/SQL
ODTUG Kaleidoscope
June 18 – 21, 2007
Pre-conference Hands-on Training - June 16 – 17
Hilton Daytona Beach Oceanfront Resort
Daytona, Florida
WOW-Wide Open World, Wide Open Web!
For more
Copyright 2000-2006 Steven Feuerstein - Pageinformation
1 visit www.odtug.com or call 910-452-7444
Making the Most of
Oracle PL/SQL
Error Management Features
Steven Feuerstein
PL/SQL Evangelist
Quest Software
[email protected]
Copyright 2000-2006 Steven Feuerstein - Page 2 m
Ten Years Writing Ten Books
on the Oracle PL/SQL Language
Deliberate
– The code architecture itself deliberately
relies on an exception. Example:exec_ddl_from_file.sql
UTL_FILE.GET_LINE get_nextline.sf
Unfortunate
– It is an error, but one that is to be
expected and may not even indicate a
fullname.pkb
Defining exceptions
Raising exceptions
Handing exceptions
Exceptions and DML
RAISE_APPLICATION_ERROR
(num binary_integer, msg varchar2,
keeperrorstack boolean default FALSE);
valerr.pkg
Copyright 2000-2006 Steven Feuerstein - Page 14 valerr2.pkg
Handling Exceptions
the
Copyright application.
2000-2006 Steven Feuerstein - Page 19
Best practices for error management
qd_error.erd
Copyright 2000-2006 Steven Feuerstein - Page 23
qd_runtime.pkb
Hard to avoid code repetition in handlers
PACKAGE errpkg
IS
PROCEDURE raise (err_in IN PLS_INTEGER);
Generic Raises PROCEDURE raise (err_in in VARCHAR2);
PROCEDURE record_and_stop (
err_in IN PLS_INTEGER := SQLCODE
,msg_in IN VARCHAR2 := NULL);
Record
and Stop
PROCEDURE record_and_continue (
err_in IN PLS_INTEGER := SQLCODE
,msg_in IN VARCHAR2 := NULL);
Record
and Continue END errpkg;
errpkg.pkg
Copyright 2000-2006 Steven Feuerstein - Page 25
Invoking standard handlers
WHEN OTHERS
THEN The developer simply
errpkg.record_and_stop; describes
END; the desired action.
Copyright 2000-2006 Steven Feuerstein - Page 26
Specifying the error
PACKAGE errnums
IS Give your
en_general_error CONSTANT NUMBER := -20000;
exc_general_error EXCEPTION; error numbers
PRAGMA EXCEPTION_INIT
(exc_general_error, -20000); names and
en_must_be_18 CONSTANT NUMBER := -20001; associate
exc_must_be_18 EXCEPTION;
PRAGMA EXCEPTION_INIT them with
(exc_must_be_18, -20001);
named
en_sal_too_low CONSTANT NUMBER := -20002;
exc_sal_too_low EXCEPTION; exceptions.
PRAGMA EXCEPTION_INIT
(exc_sal_too_low , -20002);
But don't write this
max_error_used CONSTANT NUMBER := -20002; code manually!
BEGIN
IF employee_rp.is_to_young (:new.hire_date)
THEN
RAISE_APPLICATION_ERROR (
-20175, 'You must be at least 18 years old!');
END IF;
BEGIN
IF employee_rp.is_to_young (:new.hire_date)
THEN
qd_runtime.raise_error (
'EMPLOYEE-TOO-YOUNG'
, name1_in => 'LAST_NAME'
, value1_in => :new.last_name);
END IF;
notification
Copyright of- Page
2000-2006 Steven Feuerstein "undefined
31 error."
Summary: an Exception Handling Architecture
ODTUG Kaleidoscope
June 18 – 21, 2007
Pre-conference Hands-on Training - June 16 – 17
Hilton Daytona Beach Oceanfront Resort
Daytona, Florida
WOW-Wide Open World, Wide Open Web!
For more
Copyright 2000-2006 Steven Feuerstein - Page information
34 visit www.odtug.com or call 910-452-7444