Updated March 31, 2023
Introduction to PL/SQL Exception
PL/SQL exceptions are the erroneous situation that occurred while executing the program written in PL/SQL. While we do different operations and manipulations in the program, there is a possibility the program may generate an error. In order to handle these situations, we can make the use of exception handling in PL/SQL. There is a provision to handle the exceptions that are generated in PL/SQL, which is called as exception handling. There are two types of the exceptions that can occur in PL/SQL.
Syntax of Exception and its Handling
The syntax of the exception in PL/SQL is as follows:
Statements or variables and constants to be declared in the beginning.
Commands to be executed in a stored procedure or where you write your business logic in PL/SQL.
We have to handle the exceptions in case if they arise while executing the code of the business logic.
WHEN exception 1
THEN statements to handle the exception 1
WHEN exception 2
THEN statements to handle the exception 2
WHEN exception 3
THEN statements to handle the exception 3
WHEN exception n
THEN statements to handle the exception n
THEN statements to handle the exception when it does not belongs to any of the above mentioned exceptions.
Note that in the above syntax, we can handle as many exceptions as we can and list them in WHEN this THEN that format. The default handling of exceptions is done by using WHEN others THEN code to execute on the occurrence of an exception.
Types of Exception
The exceptions are broadly classified into two types which are as specified below:
- System defined exceptions
- User-defined exceptions
1. System Defined Exceptions
PL/SQL provides already in build pre-defined exceptions, which are the most often raised exceptions. Some of them include no data found exception, access into null exception, case not found exception, the duplicate value on index exception.
In fact, the below table will provide you with the list of most often occurring exceptions along with their descriptions.
|This type of exception occurs when the cursor is used incorrectly and the operations being performed are not allowed related to the cursor, such as trying to close a cursor which has not even being opened.
|If we are trying to log in to the database using the wrong password or username, this exception is raised.
|When the PL/ SQL runs out of memory while executing the program or performing some operations in the database, this exception is raised.
|When we are trying to make an attempt of dividing a particular value with a zero number, then this exception is generated.
|This exception is generated when a member method is given the call, but the object type instance of it is not being initialized.
|This type of exception occurs when we are trying to convert the string into the numerical format and due to the content of the string being not able to traverse in the number format due to the presence of some characters or special symbols in the string.
The system-defined exceptions, also known as predefined exceptions, can be handled in the procedure using the same syntax above.
Let us consider one example where we have the table named customers_details having the following contents shown in the image below.
cust_id customers_details.customer_id%type := 99;
SELECT f_name, email_id INTO customers_name, customers_mailid
WHERE id = cust_id;
DBMS_OUTPUT.PUT_LINE ('Customer Name: '|| customers_name);
DBMS_OUTPUT.PUT_LINE ('Customer Address: ' || customers_mailid);
WHEN no_data_found THEN
dbms_output.put_line('Customer Not Found!');
WHEN others THEN
dbms_output.put_line('Sorry there is some error!');
The output of the above program is as shown below as the customer having the customer id as 99 does not exist in our customers_details table.
2. User-Defined Exceptions
We can create our own exceptions, define them in our program, and even try to handle them by using exception handling mechanisms. This necessity arises when our error situation cannot be handled by using the predefined exceptions available in PL/ SQL.
User-defined exceptions can be created by using either the RAISE statement or standard procedure of DBMS_STANDARD.RAISE_APPLICATION_ERROR.
Syntax of doing the same is as shown.
DECLARE sampleException EXCEPTION;
Let us consider one example that will help us demonstrate how we can create and handle our own user-defined exceptions.
customer_id customers_details.customer_id%type := &cc_id;
-- user defined exception
IF customer_id <= 0 THEN
SELECT name, address INTO cust_name, cust_email
WHERE id = customer_id;
DBMS_OUTPUT.PUT_LINE ('f_name: '|| cust_name);
DBMS_OUTPUT.PUT_LINE ('Email ID: ' || cust_email);
WHEN sample_Exception_invalid_id THEN
dbms_output.put_line('Provided ID should have the value more than zero');
WHEN no_data_found THEN
dbms_output.put_line('Customer not found!');
WHEN others THEN
dbms_output.put_line('Sorry! Some error occurred');
The output of the above program is as shown below because there are none of the records in the customers_details table whose customer id is 99. Hence the user-defined exception created by us got raised, and the message was displayed while handling the exception that “Provided ID should have the value more than zero”.
Exceptions occur when there is an erroneous situation arising in the PL/SQL program. PL/SQL comes with some readymade predefined system exceptions, including no data found, invalid number, divide by zero, etc. We can handle these exceptions in our program, and we can do the appropriate actions on such an exception. The other type of exceptions is the user-defined exceptions that can be created by us and can be raised in our program according to the program’s requirement. Both types of exceptions can be handled by using the exception handling mechanisms in our program.
We hope that this EDUCBA information on “PL/SQL Exception” was beneficial to you. You can view EDUCBA’s recommended articles for more information.