0% found this document useful (0 votes)
2 views

U6_ORACLE_PLSQL_2025_partial_programmer_exceptions_Students

The document discusses handling exceptions in PL/SQL using predefined structures such as SQLCODE, SQLERRM, RAISE_APPLICATION_ERROR, and EXCEPTION_INIT. It provides examples of procedures that utilize these functions to manage exceptions, including raising custom errors and handling unexpected errors. The document emphasizes the importance of proper exception management to ensure the robustness of PL/SQL programs.
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)
2 views

U6_ORACLE_PLSQL_2025_partial_programmer_exceptions_Students

The document discusses handling exceptions in PL/SQL using predefined structures such as SQLCODE, SQLERRM, RAISE_APPLICATION_ERROR, and EXCEPTION_INIT. It provides examples of procedures that utilize these functions to manage exceptions, including raising custom errors and handling unexpected errors. The document emphasizes the importance of proper exception management to ensure the robustness of PL/SQL programs.
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/ 15

PL/SQL.

Partially Programmer's exceptions


The programmer can use certain Oracle predefined structures to handle exceptions:

• Functions:
• SQLCODE
• SQLERRM
• The procedure:
• RAISE_APPLICATION_ERROR
• The pragma:
• EXCEPTION_INIT

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


PL/SQL. Partially Programmer's exceptions (SQLCODE, SQLERRM)
● Functions defined in the STANDARD package:
• SQLCODE, returns the error code associated with the last
raised exception.
function SQLCODE return NUMBER;

• SQLERRM, returns the (error) message associated with


SQLCODE.
function SQLERRM return VARCHAR2;

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


PL/SQL. Partially Programmer's exceptions (SQLCODE, SQLERRM)
...
BEGIN
...
EXCEPTION
...
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE( SQLCODE || SQLERRM );
END <programa>;

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


PL/SQL. Partially Programmer's exceptions (SQLCODE, SQLERRM)
Using exceptions
CREATE OR REPLACE PROCEDURE Increase_Salary5 ( 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 OTHERS THEN
DBMS_OUTPUT.PUT_LINE('***Error during execution ');
DBMS_OUTPUT.PUT_LINE( SQLCODE || SQLERRM );
END Increase_Salary5;

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


PL/SQL. Partially Programmer's exceptions
(RAISE_APPLICATION_ERROR)
● This procedure is defined in the packet DBMS_STANDARD
PROCEDURE RAISE_APPLICATION_ERROR
( error-cod, error-message )
error-cod: range [-20999..-20000]
error-message: maximum length string 512 bytes

• The error code must be within the allowed range.


• The error code must not be a predefined code (-1,+100,...).
• The error code must be easily recognizable (ex. -20555).

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


PL/SQL. Partially Programmer's exceptions
(RAISE_APPLICATION_ERROR)
RAISE_APPLICATION_ERROR:

• Raises the exception.


• Sets and sends the message associated with the exception.
• Undoes the changes made by the program, only if the
exception is handled in the body of the subprogram, not
in the EXCEPTION section.

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


PL/SQL. Partially Programmer's exceptions
(RAISE_APPLICATION_ERROR)
Format:
...
BEGIN
...
IF <error condition> THEN
RAISE_APPLICATION_ERROR(cod-error, mensaje);
END IF;
END <programme>;

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


PL/SQL. Partially Programmer's exceptions
(RAISE_APPLICATION_ERROR)
15. Implement a procedure that increases the commission and salary of the given worker by 100 euros.
CREATE OR REPLACE
PROCEDURE Increase_Salary6 (num_emple IN emp.empno%TYPE) IS
vsalary emp.sal%TYPE;
vextra emp.comm%TYPE;
BEGIN
SELECT sal, comm INTO vsalary, vextra FROM emp
WHERE empno = num_emple;
IF vextra IS NULL THEN
RAISE_APPLICATION_ERROR(-20888, '***commission NULL ');
ELSE
UPDATE emp SET sal = sal+100, comm = comm+100
WHERE empno = num_emple;
DBMS_OUTPUT.PUT_LINE(' Salary and commission increased ');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('*** The employee doesn’t exist ');
END Increase_Salary6; cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 8
CREATE OR REPLACE
PROCEDURE Increase_Salary6 (num_emple IN emp.empno%TYPE) IS
vsalary emp.sal%TYPE;
vextra emp.comm%TYPE;
BEGIN
SELECT sal, comm INTO vsalary, vextra FROM emp
WHERE empno = num_emple;
IF vextra IS NULL THEN
RAISE_APPLICATION_ERROR(-20888, '***commission NULL ');
ELSE
UPDATE emp SET sal = sal+100, comm = comm+100
WHERE empno = num_emple;
DBMS_OUTPUT.PUT_LINE(' Salary and commission increased ');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('*** The employee doesn’t exist ');
END Increase_Salary6; cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 9
PL/SQL. Partially Programmer's exceptions
(RAISE_APPLICATION_ERROR)
16. What happens if the WHEN OTHERS clause is added?
CREATE OR REPLACE PROCEDURE Increase_Salary7 (num_emple IN emp.empno%TYPE) IS
vsalary emp.sal%TYPE;
vextra emp.comm%TYPE;
BEGIN
SELECT sal, comm INTO vsalary, vextra FROM empl
WHERE empno = num_emple;
IF vextra IS NULL THEN
RAISE_APPLICATION_ERROR(-20888, '***commission NULL ');
ELSE
UPDATE emp SET sal = sal+100, comm = comm+100
WHERE empno = num_emple;
DBMS_OUTPUT.PUT_LINE(' Salary and commission increased ');
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('*** The employee doesn’t exist ');
WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('*** Unexpected error ');
END Increase_Salary7; cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 10
PL/SQL. Partially Programmer's exceptions
(RAISE_APPLICATION_ERROR)
• Any exception that is raised and does not have a specific
treatment is redirected to be treated by the WHEN OTHERS
clause, if it exists.
• Therefore, if the RAISE_APPLICATION_ERROR procedure is invoked
the program is redirected to the WHEN OTHERS branch.

EXECUTE Increase_Salary7(7934);
*** Unexpected error

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


PL/SQL. Partially Programmer's exceptions
(RAISE_APPLICATION_ERROR)
How to solve this issue ?
● By treating in the WHEN OTHERS clause the code of the raised
exception.
● The message corresponding to the exception will also be
handled in the WHEN OTHERS, instead of invoking the
RAISE_APPLICATION_ERROR procedure.

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


PL/SQL. Partially Programmer's exceptions
(RAISE_APPLICATION_ERROR)
17. RAISE_APPLICATION_ERROR and WHEN OTHERS

CREATE OR REPLACE PROCEDURE Increase_Salary8 (num_emple IN emp.empno%TYPE) IS


vsalary emp.sal%TYPE;
vextra emp.comm%TYPE;
BEGIN
SELECT sal, comm INTO vsalary, vextra FROM empl
WHERE empno = num_emple;
IF vextra IS NULL THEN
RAISE_APPLICATION_ERROR(-20888, '');
ELSE
UPDATE emp SET sal = sal+100, comm = comm+100
WHERE empno = num_emple;
DBMS_OUTPUT.PUT_LINE(' Salary and commission increased ');
END IF;
cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 13
PL/SQL. Partially Programmer's exceptions
(RAISE_APPLICATION_ERROR)
17. RAISE_APPLICATION_ERROR and WHEN OTHERS
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('***The employee doesn’t exist ');
WHEN OTHERS THEN
IF SQLCODE = -20888
THEN DBMS_OUTPUT.PUT_LINE('***Commission NULL ');
ELSE DBMS_OUTPUT.PUT_LINE('*** Unexpected error ');
END IF;
END Increase_Salary8;

EXECUTE Increase_Salary8(7934);
***Commission NULL
cc-by-sa Gema Cervigón. Guadalupe Bermejo UT7 DDBB 14
PL/SQL. Partially Programmer's exceptions
(RAISE_APPLICATION_ERROR)
18. Implement another procedure that also raises the salary and
commission of the employee's manager.

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

You might also like