PL-SQL
Oracle PL/SQL Trigger Tutorial: Instead of, Compound [Example]
What is Trigger in PL/SQL? TRIGGERS are stored programs that are fired by Oracle engine...
An exception occurs when the PL/SQL engine encounters an instruction which it cannot execute due to an error that occurs at run-time. These errors will not be captured at the time of compilation and hence these needed to handle only at the run-time.
For example, if PL/SQL engine receives an instruction to divide any number by '0', then the PL/SQL engine will throw it as an exception. The exception is only raised at the run-time by the PL/SQL engine.
Exceptions will stop the program from executing further, so to avoid such condition, they need to be captured and handled separately. This process is called as Exception-Handling, in which the programmer handles the exception that can occur at the run time.
In this tutorial, you will learn the following topics-
Exceptions are handled at the block, level, i.e., once if any exception occurs in any block then the control will come out of execution part of that block. The exception will then be handled at the exception handling part of that block. After handling the exception, it is not possible to resend control back to the execution section of that block.
The below syntax explains how to catch and handle the exception.
BEGIN <execution block> . . EXCEPTION WHEN <exceptionl_name> THEN <Exception handling code for the “exception 1 _name’' > WHEN OTHERS THEN <Default exception handling code for all exceptions > END;
Syntax Explanation:
Note: WHEN OTHERS should always be at the last position of the sequence. The exception handling part present after WHEN OTHERS will never get executed as the control will exit from the block after executing the WHEN OTHERS.
There are two types of Exceptions in Pl/SQL.
Oracle has predefined some common exception. These exceptions have a unique exception name and error number. These exceptions are already defined in the 'STANDARD' package in Oracle. In code, we can directly use these predefined exception name to handle them.
Below are the few predefined exceptions
| Exception | Error Code | Exception Reason |
| ACCESS_INTO_NULL | ORA-06530 | Assign a value to the attributes of uninitialized objects |
| CASE_NOT_FOUND | ORA-06592 | None of the 'WHEN' clause in CASE statement satisfied and no 'ELSE' clause is specified |
| COLLECTION_IS_NULL | ORA-06531 | Using collection methods (except EXISTS) or accessing collection attributes on a uninitialized collections |
| CURSOR_ALREADY_OPEN | ORA-06511 | Trying to open a cursor which is already opened |
| DUP_VAL_ON_INDEX | ORA-00001 | Storing a duplicate value in a database column that is a constrained by unique index |
| INVALID_CURSOR | ORA-01001 | Illegal cursor operations like closing an unopened cursor |
| INVALID_NUMBER | ORA-01722 | Conversion of character to a number failed due to invalid number character |
| NO_DATA_FOUND | ORA-01403 | When 'SELECT' statement that contains INTO clause fetches no rows. |
| ROW_MISMATCH | ORA-06504 | When cursor variable data type is incompatible with the actual cursor return type |
| SUBSCRIPT_BEYOND_COUNT | ORA-06533 | Referring collection by an index number that is larger than the collection size |
| SUBSCRIPT_OUTSIDE_LIMIT | ORA-06532 | Referring collection by an index number that is outside the legal range (eg: -1) |
| TOO_MANY_ROWS | ORA-01422 | When a 'SELECT' statement with INTO clause returns more than one row |
| VALUE_ERROR | ORA-06502 | Arithmetic or size constraint error (eg: assigning a value to a variable that is larger than the variable size) |
| ZERO_DIVIDE | ORA-01476 | Dividing a number by '0' |
In Oracle, other than the above-predefined exceptions, the programmer can create their own exception and handle them. They can be created at a subprogram level in the declaration part. These exceptions are visible only in that subprogram. The exception that is defined in the package specification is public exception, and it is visible wherever the package is accessible. <
Syntax: At subprogram level
DECLARE <exception_name> EXCEPTION; BEGIN <Execution block> EXCEPTION WHEN <exception_name> THEN <Handler> END;
Syntax:At Package Specification level
CREATE PACKAGE <package_name> IS <exception_name> EXCEPTION; . . END <package_name>;
All the predefined exceptions are raised implicitly whenever the error occurs. But the user-defined exceptions needs to be raised explicitly. This can be achieved using the keyword 'RAISE'. This can be used in any of the ways mentioned below.
If 'RAISE' is used separately in the program, then it will propagate the already raised exception to the parent block. Only in exception block can be used as shown below.
CREATE [ PROCEDURE | FUNCTION ]
AS
BEGIN
<Execution block>
EXCEPTION
WHEN <exception_name> THEN
<Handler>
RAISE;
END;
Syntax Explanation:
Note: While raising the exception to the parent block the exception that is getting raised should also be visible at parent block, else oracle will throw an error.
CREATE [ PROCEDURE | FUNCTION ] AS BEGIN <Execution block> RAISE <exception_name> EXCEPTION WHEN <exception_name> THEN <Handler> END;
Syntax Explanation:
Example 1: In this example, we are going to see
DECLARE Sample_exception EXCEPTION; PROCEDURE nested_block IS BEGIN Dbms_output.put_line(‘Inside nested block’); Dbms_output.put_line(‘Raising sample_exception from nested block’); RAISE sample_exception; EXCEPTION WHEN sample_exception THEN Dbms_output.put_line (‘Exception captured in nested block. Raising to main block’); RAISE, END; BEGIN Dbms_output.put_line(‘Inside main block’); Dbms_output.put_line(‘Calling nested block’); Nested_block; EXCEPTION WHEN sample_exception THEN Dbms_output.put_line (‘Exception captured in main block'); END: /
Code Explanation:
After this chapter. you should be able to work for the following aspects of Pl SQL exceptions
What is Trigger in PL/SQL? TRIGGERS are stored programs that are fired by Oracle engine...
What is Identifiers? Identifiers are nothing but a name that is given to a PL/SQL object. The...
$20.20 $9.99 for today 4.5 (108 ratings) Key Highlights of PL/SQL Tutorial PDF 188+ pages eBook...
What is Collection? A Collection is an ordered group of elements of particular data types. It can...
In this tutorial, we will introduce SQL* Plus and learn how to connect it to the database. After...
What is PL/SQL? Oracle PL/SQL is an extension of SQL language that combines the data manipulation power...