Download EXCEPTION

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Exceptions
Oracle Database PL/SQL 10g Programming
Chapter 7
Exceptions





2006
Exception Handling
Declaring Exceptions
Raising Exceptions
Handling Exceptions
Error Stack Management
Oracle Database PL/SQL 10g Programming
(Chapter 7)
Page 2
Exceptions
Exception Handling: Definition


2006
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)
Page 3
Exceptions
Exception Handling: Error Types

Compile-time Errors



Run-time Errors


2006
Raised errors due to syntax mistakes
Raised errors due to reserved word use
Raised by logical programming mistakes
Raised by unexpected data states
Oracle Database PL/SQL 10g Programming
(Chapter 7)
Page 4
Exceptions
Declaring Exceptions: Types

Predefined Exceptions

Standard event exceptions are defined, like:




Package event exceptions are defined, like:



INVALID_DIRECTORY (DBMS_LOB package)
INCONSISTENT_TYPE (DBMS_SQL package)
User-defined Exceptions


2006
NO_DATA_FOUND
NOT_LOGGED_ON
TOO_MANY_ROWS
Are defined in anonymous or named blocks
Are best placed in package specifications
Oracle Database PL/SQL 10g Programming
(Chapter 7)
Page 5
Exceptions
Declaring Exceptions: Predefined

Predefined Exceptions

Have an assigned Oracle error number.


Have an assigned Oracle exception name.


DUP_VAL_ON_INDEX
Have an assigned Oracle description.

2006
ORA-0001
Unique constraint violated.
Oracle Database PL/SQL 10g Programming
(Chapter 7)
Page 6
Exceptions
Declaring Exceptions: Syntax
DECLARE
user_defined_error EXCEPTION;
BEGIN
… shown_in_later_example …
END;
/
2006
Oracle Database PL/SQL 10g Programming
(Chapter 7)
Page 7
Exceptions
Declaring 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.

2006
Oracle Database PL/SQL 10g Programming
(Chapter 7)
Page 8
Exceptions
Declaring 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;
/
2006
Oracle Database PL/SQL 10g Programming
(Chapter 7)
Page 9
Exceptions
Raising Exceptions: Types

Raising a user-defined exception:




Raising a user-defined application exception:

2006
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.
Declare and raise simultaneously an error in the execution
section by using the:
RAISE_APPLICATION_EXCEPTION()
Oracle Database PL/SQL 10g Programming
(Chapter 7)
Page 10
Exceptions
Raising Exceptions: Simple Syntax
DECLARE
user_defined_error EXCEPTION;
BEGIN
IF condition_1 <> condition_2 THEN
RAISE user_defined_error;
END IF;
EXCEPTION
… shown_in_later_example …
END;
/
2006
Oracle Database PL/SQL 10g Programming
(Chapter 7)
Page 11
Exceptions
Raising Exceptions: Application Syntax
BEGIN
IF 1 <> 2 THEN
RAISE_APPLICATION_ERROR(-20001,'user message');
END IF;
EXCEPTION
… shown_in_later_example …
END;
/
2006
Oracle Database PL/SQL 10g Programming
(Chapter 7)
Page 12
Exceptions
Handling Exceptions: Definition





2006
The EXCEPTION section handles 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.
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)
Page 13
Exceptions
Handling Exceptions: Simple Syntax
DECLARE
user_defined_error EXCEPTION;
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;
/
2006
Oracle Database PL/SQL 10g Programming
(Chapter 7)
Page 14
Exceptions
Handling 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;
/
2006
Oracle Database PL/SQL 10g Programming
(Chapter 7)
Page 15
Exceptions
Error Stack Management: Definition


2006
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)
Page 16
Exceptions
Error Stack Management: Stack Dependency
BEGIN
BEGIN
… programming detail …
BEGIN
… programming detail …
EXCEPTION
WHEN inner_most THEN
… programming detail …
END;
EXCEPTION
WHEN inner_most THEN
… programming detail …
END;
EXCEPTION
WHEN inner_most THEN
… programming detail …
END;
2006
Oracle Database PL/SQL 10g Programming
(Chapter 7)
Page 17
Summary





2006
Exception Handling
Declaring Exceptions
Raising Exceptions
Handling Exceptions
Error Stack Management
Oracle Database PL/SQL 10g Programming
(Chapter 7)
Page 18
Related documents