180 likes | 198 Views
Learn about exception handling in Oracle PL/SQL, including declaring and raising exceptions, mapping predefined exceptions, and handling errors in your code.
E N D
Exceptions • Exception Handling • Declaring Exceptions • Raising Exceptions • Handling Exceptions • Error Stack Management Oracle Database PL/SQL 10g Programming (Chapter 7)
ExceptionsException Handling: Definition • Exceptions are failures in a programs compilation or execution. • Exceptions can be critical or non-critical failures, the former should stop execution of the program and undo changes while the latter may be recorded and examined later. Oracle Database PL/SQL 10g Programming (Chapter 7)
ExceptionsException Handling: Error Types • Compile-time Errors • Raised errors due to syntax mistakes • Raised errors due to reserved word use • Run-time Errors • Raised by logical programming mistakes • Raised by unexpected data states Oracle Database PL/SQL 10g Programming (Chapter 7)
ExceptionsDeclaring Exceptions: Types • Predefined Exceptions • Standard event exceptions are defined, like: • NO_DATA_FOUND • NOT_LOGGED_ON • TOO_MANY_ROWS • Package event exceptions are defined, like: • INVALID_DIRECTORY (DBMS_LOB package) • INCONSISTENT_TYPE (DBMS_SQL package) • User-defined Exceptions • Are defined in anonymous or named blocks • Are best placed in package specifications Oracle Database PL/SQL 10g Programming (Chapter 7)
ExceptionsDeclaring Exceptions: Predefined • Predefined Exceptions • Have an assigned Oracle error number. • ORA-0001 • Have an assigned Oracle exception name. • DUP_VAL_ON_INDEX • Have an assigned Oracle description. • Unique constraint violated. Oracle Database PL/SQL 10g Programming (Chapter 7)
ExceptionsDeclaring Exceptions: Syntax DECLARE user_defined_errorEXCEPTION; BEGIN … shown_in_later_example … END; / Oracle Database PL/SQL 10g Programming (Chapter 7)
ExceptionsDeclaring Exceptions: Mapping Predefined • Mapping Predefined Exceptions • Declare an EXCEPTION variable. • Use the PRAGMA (precompiler instruction) EXCEPTION_INIT to map a user-defined EXCEPTION to a predefined error number. • Can increase readability of programs. Oracle Database PL/SQL 10g Programming (Chapter 7)
ExceptionsDeclaring Exceptions: Mapping Syntax DECLARE user_defined_error EXCEPTION; PRAGMA EXCEPTION_INIT(user_defined_error,-1400); BEGIN -- Assuming NOT NULL constraints this raises an error. INSERT INTO a_table (id, name) VALUES (NULL,NULL); EXCEPTION WHEN user_defined_error THEN INSERT INTO a_log_table VALUES ('ORA-01400 against A_TABLE'); END; / Oracle Database PL/SQL 10g Programming (Chapter 7)
ExceptionsRaising Exceptions: Types • Raising a user-defined exception: • Declare a variable as an EXCEPTION data type in the declaration section. • Call the RAISE command and variable name in the execution section. • User-defined exceptions are called explicitly as opposed to predefined exceptions that are raised implicitly. • Raising a user-defined application exception: • Declare and raise simultaneously an error in the execution section by using the: RAISE_APPLICATION_EXCEPTION() Oracle Database PL/SQL 10g Programming (Chapter 7)
ExceptionsRaising Exceptions: Simple Syntax DECLARE user_defined_errorEXCEPTION; BEGIN IF condition_1 <> condition_2 THEN RAISE user_defined_error; END IF; EXCEPTION … shown_in_later_example … END; / Oracle Database PL/SQL 10g Programming (Chapter 7)
ExceptionsRaising Exceptions: Application Syntax BEGIN IF 1 <> 2 THEN RAISE_APPLICATION_ERROR(-20001,'user message'); END IF; EXCEPTION … shown_in_later_example … END; / Oracle Database PL/SQL 10g Programming (Chapter 7)
ExceptionsHandling Exceptions: Definition • The EXCEPTION section handles exceptions. • The WHENerror_name or OTHERS captures thrown exceptions. • Further handling can occur inside the WHEN block, like validating an error number to branch handling procedures. • The RETURN command can be used after handling the exception to return the line below where the error occurred. • Unhandled exceptions will raise the thrown exception to the calling program. Oracle Database PL/SQL 10g Programming (Chapter 7)
ExceptionsHandling Exceptions: Simple Syntax DECLARE user_defined_errorEXCEPTION; BEGIN IF condition_1 <> condition_2 THEN RAISE user_defined_error; END IF; EXCEPTION WHEN user_defined_error THEN INSERT INTO a_log_table VALUES ('ORA-01400 against A_TABLE'); END; / Oracle Database PL/SQL 10g Programming (Chapter 7)
ExceptionsHandling Exceptions: Application Syntax BEGIN IF 1 <> 2 THEN RAISE_APPLICATION_ERROR(-20001,'user message'); END IF; EXCEPTION WHEN OTHERS THEN IF SQLCODE = -20001 THEN dbms_output.put_line('Expected.'); RETURN; ELSE RAISE_APPLICATION_ERROR(-20002,'Unanticipated!'); END IF; END; / Oracle Database PL/SQL 10g Programming (Chapter 7)
ExceptionsError Stack Management: Definition • The error stack is the sequencing of errors from the triggering event to the calling block of code, which can be a SQL statement in SQL*Plus. • The DBMS_UTILITY package now has a FORMAT_ERROR_BACKTRACE procedure that enables you to capture the complete error stack. Oracle Database PL/SQL 10g Programming (Chapter 7)
ExceptionsError Stack Management: Stack Dependency Oracle Database PL/SQL 10g Programming (Chapter 7)
Summary • Exception Handling • Declaring Exceptions • Raising Exceptions • Handling Exceptions • Error Stack Management Oracle Database PL/SQL 10g Programming (Chapter 7)