Download presentation
Presentation is loading. Please wait.
Published byReynold Brian Patterson Modified over 9 years ago
1
Exceptions Oracle Database PL/SQL 10g Programming Chapter 7
2
2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 2 Exceptions Exception Handling Exception Handling Declaring Exceptions Declaring Exceptions Raising Exceptions Raising Exceptions Handling Exceptions Handling Exceptions Error Stack Management Error Stack Management
3
2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 3 Exceptions Exception Handling: Definition Exceptions are failures in a programs compilation or execution. 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. 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.
4
2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 4 Exceptions Exception Handling: Error Types Compile-time Errors Compile-time Errors Raised errors due to syntax mistakes Raised errors due to syntax mistakes Raised errors due to reserved word use Raised errors due to reserved word use Run-time Errors Run-time Errors Raised by logical programming mistakes Raised by logical programming mistakes Raised by unexpected data states Raised by unexpected data states
5
2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 5 Exceptions Declaring Exceptions: Types Predefined Exceptions Predefined Exceptions Standard event exceptions are defined, like: Standard event exceptions are defined, like: NO_DATA_FOUND NO_DATA_FOUND NOT_LOGGED_ON NOT_LOGGED_ON TOO_MANY_ROWS TOO_MANY_ROWS Package event exceptions are defined, like: Package event exceptions are defined, like: INVALID_DIRECTORY (DBMS_LOB package) INVALID_DIRECTORY (DBMS_LOB package) INCONSISTENT_TYPE (DBMS_SQL package) INCONSISTENT_TYPE (DBMS_SQL package) User-defined Exceptions User-defined Exceptions Are defined in anonymous or named blocks Are defined in anonymous or named blocks Are best placed in package specifications Are best placed in package specifications
6
2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 6 Exceptions Declaring Exceptions: Predefined Predefined Exceptions Predefined Exceptions Have an assigned Oracle error number. Have an assigned Oracle error number. ORA-0001 ORA-0001 Have an assigned Oracle exception name. Have an assigned Oracle exception name. DUP_VAL_ON_INDEX DUP_VAL_ON_INDEX Have an assigned Oracle description. Have an assigned Oracle description. Unique constraint violated. Unique constraint violated.
7
2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 7 Exceptions Declaring Exceptions: Syntax DECLARE user_defined_error EXCEPTION; user_defined_error EXCEPTION;BEGIN … shown_in_later_example … … shown_in_later_example …END;/
8
2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 8 Exceptions Declaring Exceptions: Mapping Predefined Mapping Predefined Exceptions Mapping Predefined Exceptions Declare an EXCEPTION variable. Declare an EXCEPTION variable. Use the PRAGMA (precompiler instruction) EXCEPTION_INIT to map a user-defined EXCEPTION to a predefined error number. Use the PRAGMA (precompiler instruction) EXCEPTION_INIT to map a user-defined EXCEPTION to a predefined error number. Can increase readability of programs. Can increase readability of programs.
9
2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 9 Exceptions Declaring Exceptions: Mapping Syntax DECLARE user_defined_error EXCEPTION; user_defined_error EXCEPTION; PRAGMA EXCEPTION_INIT(user_defined_error,-1400); PRAGMA EXCEPTION_INIT(user_defined_error,-1400);BEGIN -- Assuming NOT NULL constraints this raises an error. -- Assuming NOT NULL constraints this raises an error. INSERT INTO a_table (id, name) VALUES (NULL,NULL); INSERT INTO a_table (id, name) VALUES (NULL,NULL);EXCEPTION WHEN user_defined_error THEN WHEN user_defined_error THEN INSERT INTO a_log_table VALUES INSERT INTO a_log_table VALUES ('ORA-01400 against A_TABLE'); ('ORA-01400 against A_TABLE');END;/
10
2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 10 Exceptions Raising Exceptions: Types Raising a user-defined exception: Raising a user-defined exception: Declare a variable as an EXCEPTION data type in the declaration section. Declare a variable as an EXCEPTION data type in the declaration section. Call the RAISE command and variable name in the execution 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. User-defined exceptions are called explicitly as opposed to predefined exceptions that are raised implicitly. Raising a user-defined application exception: Raising a user-defined application exception: Declare and raise simultaneously an error in the execution section by using the: Declare and raise simultaneously an error in the execution section by using the:RAISE_APPLICATION_EXCEPTION()
11
2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 11 Exceptions Raising Exceptions: Simple Syntax DECLARE user_defined_error EXCEPTION; user_defined_error EXCEPTION;BEGIN IF condition_1 <> condition_2 THEN IF condition_1 <> condition_2 THEN RAISE user_defined_error; RAISE user_defined_error; END IF; END IF;EXCEPTION … shown_in_later_example … … shown_in_later_example …END;/
12
2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 12 Exceptions Raising Exceptions: Application Syntax BEGIN IF 1 <> 2 THEN IF 1 <> 2 THEN RAISE_APPLICATION_ERROR(-20001,'user message'); RAISE_APPLICATION_ERROR(-20001,'user message'); END IF; END IF;EXCEPTION … shown_in_later_example … … shown_in_later_example …END;/
13
2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 13 Exceptions Handling Exceptions: Definition The EXCEPTION section handles exceptions. The EXCEPTION section handles exceptions. The WHEN error_name or OTHERS captures thrown exceptions. The WHEN error_name or OTHERS captures thrown exceptions. Further handling can occur inside the WHEN block, like validating an error number to branch handling procedures. 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. 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. Unhandled exceptions will raise the thrown exception to the calling program.
14
2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 14 Exceptions Handling Exceptions: Simple Syntax DECLARE user_defined_error EXCEPTION; user_defined_error EXCEPTION;BEGIN IF condition_1 <> condition_2 THEN IF condition_1 <> condition_2 THEN RAISE user_defined_error; RAISE user_defined_error; END IF; END IF;EXCEPTION WHEN user_defined_error THEN WHEN user_defined_error THEN INSERT INTO a_log_table VALUES INSERT INTO a_log_table VALUES ('ORA-01400 against A_TABLE'); ('ORA-01400 against A_TABLE');END;/
15
2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 15 Exceptions Handling Exceptions: Application Syntax BEGIN IF 1 <> 2 THEN IF 1 <> 2 THEN RAISE_APPLICATION_ERROR(-20001,'user message'); RAISE_APPLICATION_ERROR(-20001,'user message'); END IF; END IF;EXCEPTION WHEN OTHERS THEN WHEN OTHERS THEN IF SQLCODE = -20001 THEN IF SQLCODE = -20001 THEN dbms_output.put_line('Expected.'); dbms_output.put_line('Expected.'); RETURN; RETURN; ELSE ELSE RAISE_APPLICATION_ERROR(-20002,'Unanticipated!'); RAISE_APPLICATION_ERROR(-20002,'Unanticipated!'); END IF; END IF;END;/
16
2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 16 Exceptions Error 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 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. The DBMS_UTILITY package now has a FORMAT_ERROR_BACKTRACE procedure that enables you to capture the complete error stack.
17
2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 17 Exceptions Error Stack Management: Stack Dependency
18
2006 Oracle Database PL/SQL 10g Programming (Chapter 7)Page 18 Summary Exception Handling Exception Handling Declaring Exceptions Declaring Exceptions Raising Exceptions Raising Exceptions Handling Exceptions Handling Exceptions Error Stack Management Error Stack Management
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.