0% found this document useful (0 votes)
6 views14 pages

U6 ORACLE PLSQL 2025 Programmer Exceptions Students

PL/SQL handles errors through exceptions, allowing programs to continue running by using exception handlers. Programmers can define their own exceptions and handle various error types using a structured syntax. Effective error handling involves anticipating potential issues, validating input data, and utilizing predefined exceptions for robust program control.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views14 pages

U6 ORACLE PLSQL 2025 Programmer Exceptions Students

PL/SQL handles errors through exceptions, allowing programs to continue running by using exception handlers. Programmers can define their own exceptions and handle various error types using a structured syntax. Effective error handling involves anticipating potential issues, validating input data, and utilizing predefined exceptions for robust program control.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

PL/SQL.

Exceptions

There is nothing more exhilarating


than to be shot at without result.
(Winston Churchill)

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 1


PL/SQL. Exceptions
PL/SQL treats all errors that occur in an anonymous block, procedure, or function as
exceptions. The exceptions can have different causes such as coding mistakes, bugs,
even hardware failures.

It is not possible to anticipate all potential exceptions, however, you can write code to
handle exceptions to enable the program to continue running as normal.

The code that you write to handle exceptions is called an exception handler.

A PL/SQL block can have an exception-handling section, which can have one or more
exception handlers.

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 2


PL/SQL. Exceptions
How to avoid errors ?
● Define the data handled by the programme of the most appropriate type.
● Carry out an exhaustive check of the input data, to make sure that they are within the
permitted range of values.
● Take into account all the possible causes of error, exceptions, contemplated by the language.
Most frequent causes of errors:
● Invalid manipulation operations. Example: Insertion of records with duplicate primary key,
deletion of records with foreign key…
● Operations with invalid data. Example: Using zero as divider
● Illegal operations, f.i. not allowed by the language. Example: the implementation of an implicit
cursor when it is possible that zero/several rows are returned in the associated query.

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 3


PL/SQL. Exceptions
● Basic syntax of the exception-handling section:
BEGIN
-- executable section
...
-- exception-handling section
EXCEPTION
WHEN e1 THEN
-- exception_handler1
WHEN e2 THEN
-- exception_handler1
WHEN OTHERS THEN
-- other_exception_handler
END;
In this syntax, e1, e2 are exceptions.

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 4


PL/SQL. Exceptions vs Error messages (from ChatGPT)
Error handling with exceptions Error handling with messages
● Exceptions provide a structured and flexible mechanism ● When the program detects a problem notifies
for handling errors more robustly and controlledly. the user about the situation through a
● Exceptions allow the program to gracefully recover from descriptive message.
errors, whether by retrying an operation, undoing ● These messages can be helpful in informing the
changes, closing connections, etc. user about what went wrong and may provide
● Exceptions can be nested and handle different levels guidance on how to correct the issue.
of errors within an application, providing greater control ● However, this approach does not offer a
over how different types of issues are handled. structured way to handle the program's
● PL/SQL provides a wide range of predefined exceptions control flow after the error occurs.
that can be caught and handled within ● Additionally, it can result in a less user-friendly
BEGIN...EXCEPTION...END blocks, as well as the ability to experience if error messages are unclear or do
define custom exceptions using the EXCEPTION not provide enough information about the
statement and the RAISE command. nature of the problem.
● It offers a more structured and controlled way to ● Error handling with messages is simpler and
handle errors, allowing for specific actions to be straightforward but lacks the ability to handle
errors more sophisticatedly and take
taken in response to them and facilitating a more
specific actions in response to them.
user-friendly and robust user experience.
cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 5
PL/SQL. Exceptions
● In PL/SQL, error handling during the execution of a program is done through exceptions.
○ An exception is an error control mechanism.
○ This mechanism allows the executing program to
■ Finish orderly if an error occurs.
■ Undo the changes made by the program so far, ONLY if the exception is handled
with the RAISE_APPLICATION_ERROR procedure.
● Exceptions are treated in the section: EXCEPTION
○ When an error occurs, an exception is raised and the program:
■ If the EXCEPTION section exists, it goes to it, to carry out the relevant handling.
■ If it does not exist, it terminates immediately, in a totally uncontrolled way.

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 6


PL/SQL. Exceptions. Types
● Fully defined by the programmer: RAISE
● Partially defined by the programmer,
○ using the functions: SQLCODE and SQLERRM
○ using the procedure: RAISE_APPLICATION_ERROR
○ using the pragma: EXCEPTION_INIT
● Defined by Oracle (internal exceptions). With system predefined name.
DUP_VAL_ON_INDEX, NO_DATA_FOUND, TOO_MANY_ROWS, etc.

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 7


PL/SQL. Programmer's exceptions. RAISE
● The programmer must define as many exceptions as necessary to cover the
particular problems of a programme.
● Algorithm
○ Define them in the declarative part of the PL/SQL block.
○ Raise them, within the program, where appropriate using the command:
RAISE.
○ Process them in section: EXCEPTION.
● It is recommended to always include the WHEN OTHERS clause.
○ WHEN OTHERS allows you to handle other exceptions that may occur and
have not been considered.

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 8


PL/SQL. Programmer's exceptions. RAISE
... AS
...

<excepción-1> EXCEPTION; -- Declare


BEGIN
...

RAISE <excepción-1>; -- Raise


EXCEPTION
WHEN <excepción-1> THEN -- Treat
<instrucciones-1>;
...

[WHEN OTHERS]
<instrucciones-x>
END <programa>; cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 9
PL/SQL. Programmer's exceptions. RAISE
14. Implement a procedure that increases by 5% the salary of workers in a given
department. What happens when a NON-EXISTING
Basic implementation department number is given? The
CREATE OR REPLACE programme reports a missing upload.
PROCEDURE Increase_Salary (ndepart IN Emp.deptno%TYPE) IS
BEGIN
UPDATE Emp
SET sal = sal*1.05
WHERE deptno = ndepart;
DBMS_OUTPUT.PUT_LINE(' Increased salary ');
END Increase_Salary;

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 10


PL/SQL. Programmer's exceptions RAISE
Enhanced implementation
CREATE OR REPLACE
PROCEDURE Increase_Salary2 ( ndepart IN emp.deptno%TYPE ) IS
BEGIN
IF ndepart = 10 OR ndepart = 20 OR
ndepart = 30 OR ndepart = 40
THEN
UPDATE Emp SET sal = sal*1.05
WHERE deptno = ndepart;
DBMS_OUTPUT.PUT_LINE(' Increased salary ');
ELSE
DBMS_OUTPUT.PUT_LINE('***Error on depart[10,20,30,40]');
END IF;
END Increase_Salary2;

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 11


PL/SQL. Programmer's exceptions. RAISE
Using exceptions
CREATE OR REPLACE
PROCEDURE Increase_Salary3 ( ndepart IN emp.deptno%TYPE ) IS
DEPART_NO_VALID EXCEPTION;
BEGIN
IF ndepart=10 OR ndepart=20 OR ndepart=30 OR ndepart=40 THEN
UPDATE Emp SET sal = sal*1.05
WHERE deptno = ndepart;
DBMS_OUTPUT.PUT_LINE(' Increased salary ');
ELSE
RAISE DEPART_NO_VALID;
END IF;
EXCEPTION
WHEN DEPART_NO_VALID THEN
DBMS_OUTPUT.PUT_LINE('***Error on depart[10,20,30,40]');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('***Error during execution ');
END Increase_Salary3;

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 12


PL/SQL. Programmer's exceptions. RAISE
EXECUTE Increase_Salary3( 10 );
Increased salary

EXECUTE Increase_Salary3( 15 )
***Error on depart[10,20,30,40]

cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 13


PL/SQL. Programmer's exceptions. RAISE
Using exceptions
CREATE OR REPLACE
PROCEDURE Increase_Salary4 ( ndepart IN emp.deptno%TYPE ) IS
DEPART_NO_VALID EXCEPTION; Which exception is raised?
BEGIN
IF ndepart=10 OR ndepart=20 OR ndepart=30 OR ndepart=40 THEN
UPDATE Emp SET sal = sal*1.05
WHERE deptno = ndepart;
DBMS_OUTPUT.PUT_LINE(' Increased salary ');
ELSE
RAISE DEPART_NO_VALID;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('*** NO DATA FOUND ****]');
WHEN DEPART_NO_VALID THEN
DBMS_OUTPUT.PUT_LINE('***Error on depart[10,20,30,40]');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('***Error during execution ');
END Increase_Salary4;
cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 14

You might also like