0% found this document useful (0 votes)
18 views5 pages

DMS Prac 23

Uploaded by

tanajifaiz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

DMS Prac 23

Uploaded by

tanajifaiz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Department of Computer Engineering : Academic Year 2024 – 2025

DATABASE MANAGEMENT SYSTEM (313315)

Practical 23: Implement PL/SQL program based on Exception


Handling (User-defined exceptions)
Subject: Database Management System Subject Code: 313301
Semester: 03 Course: CO3K-A
Laboratory No: L003 Name of Subject Teacher: Mrs. Apurva
Sumeet Wadekar
Name of Student: Tejal Abhay Patne Roll ID: 23203A0050

Experiment No 23
Title of Experiment Implement PL/SQL program based on Exception Handling (User-defined
exceptions)

I. Practical Significance and theoretical background:


PL/SQL allows you to define your own exceptions according to the
need of your program. A user-defined exception must be declared and then raised
explicitly,
using either a RAISE statement or the procedure
DBMS_STANDARD.RAISE_APPLICATION_ERROR

 .User defined Exceptions

This type of users can create their own exceptions according to


the need and to raise these exceptions explicitly raise command is used. Example:
Divide
non-negative integer x by y such that the result is greater than or equal to 1. From the
given
question we can conclude that there exist two exceptions Division by zero. If result is
greater than or equal to 1 means y is less than or equal to x.
DECLARE
x int:=&x; /*taking value at run time*/
y int:=&y;
div_r float;

exp1 EXCEPTION;
exp2 EXCEPTION;
BEGIN
IF y=0 then
raise exp1;
ELSEIF y > x then
raise exp2;
ELSE
div_r:= x / y;
Department of Computer Engineering : Academic Year 2024 – 2025
DATABASE MANAGEMENT SYSTEM (313315)
dbms_output.put_line('the result is '||div_r);
END IF;
EXCEPTION
WHEN exp1 THEN
dbms_output.put_line('Error');
dbms_output.put_line('division by zero not allowed');
WHEN exp2 THEN
dbms_output.put_line('Error');
dbms_output.put_line('y is greater than x please check the input');
END;

II. Procedure:
SET SERVEROUTPUT ON;

DECLARE
insufficient_funds EXCEPTION;
invalid_amount EXCEPTION;

account_balance NUMBER := 1000;


deposit_amount NUMBER;
withdrawal_amount NUMBER;

PROCEDURE deposit(p_amount NUMBER) IS


BEGIN
IF p_amount <= 0 THEN
RAISE invalid_amount;
ELSE
account_balance := account_balance + p_amount;
DBMS_OUTPUT.PUT_LINE('Deposit successful. New balance: ' ||
account_balance);
END IF;
EXCEPTION
WHEN invalid_amount THEN
DBMS_OUTPUT.PUT_LINE('Error: Deposit amount must be greater
than zero.');
END deposit;

PROCEDURE withdraw(p_amount NUMBER) IS


BEGIN
IF p_amount <= 0 THEN
RAISE invalid_amount;
ELSIF p_amount > account_balance THEN
RAISE insufficient_funds;
Department of Computer Engineering : Academic Year 2024 – 2025
DATABASE MANAGEMENT SYSTEM (313315)
ELSE
account_balance := account_balance - p_amount;
DBMS_OUTPUT.PUT_LINE('Withdrawal successful. New balance: '
|| account_balance);
END IF;
EXCEPTION
WHEN invalid_amount THEN
DBMS_OUTPUT.PUT_LINE('Error: Withdrawal amount must be
greater than zero.');
WHEN insufficient_funds THEN
DBMS_OUTPUT.PUT_LINE('Error: Insufficient funds for this
withdrawal.');
END withdraw;

BEGIN
deposit(500);
deposit(-100);
withdraw(300);
withdraw(1500);
withdraw(-200);

DBMS_OUTPUT.PUT_LINE('Final account balance: ' || account_balance);

EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An unexpected error has occurred: ' ||
SQLERRM);
END;
III. Observations/Output:
Department of Computer Engineering : Academic Year 2024 – 2025
DATABASE MANAGEMENT SYSTEM (313315)

IV. Conclusion:

This PL/SQL program demonstrates effective exception handling through user-defined


exceptions in a bank account management context. Key takeaways include:

1. Custom Exception Handling: User-defined exceptions (insufficient_funds and


invalid_amount) manage specific error conditions.

2. Separation of Concerns: Encapsulating deposit and withdrawal logic in separate


procedures improves readability and maintainability.

3. Robustness: The program checks for invalid inputs and insufficient funds,
providing informative error messages.

4. Flexibility: The structure allows for easy expansion to include more banking
operations.

5. Error Reporting: Immediate feedback via DBMS_OUTPUT.PUT_LINE helps


users understand transaction outcomes.
Overall, this experiment highlights the importance of robust exception handling in PL/SQL
for a user-friendly experience.

V. Questions:
1. How to define the exception?

To define a user-defined exception, you simply declare it in the PL/SQL


block or package. The syntax is straightforward:
DECLARE
my_exception EXCEPTION; -- Define the exception
BEGIN
-- Your logic here
END;

2. Create the Procedure with the help of user defined exception.


Below is an example of a PL/SQL procedure that uses a user-defined
exception to manage a bank account withdrawal operation.

SET SERVEROUTPUT ON;

DECLARE
insufficient_funds EXCEPTION; -- User-defined exception
Department of Computer Engineering : Academic Year 2024 – 2025
DATABASE MANAGEMENT SYSTEM (313315)

PROCEDURE withdraw(p_amount NUMBER) IS


account_balance NUMBER := 1000; -- Sample initial balance
BEGIN
IF p_amount <= 0 THEN
RAISE insufficient_funds; -- Raise exception for invalid
withdrawal
ELSIF p_amount > account_balance THEN
RAISE insufficient_funds; -- Raise exception for insufficient
funds
ELSE
account_balance := account_balance - p_amount; -- Update
balance
DBMS_OUTPUT.PUT_LINE('Withdrawal successful. New
balance: ' || account_balance);
END IF;

EXCEPTION
WHEN insufficient_funds THEN
DBMS_OUTPUT.PUT_LINE('Error: Insufficient funds or invalid
withdrawal amount.');
END withdraw;

BEGIN
-- Test the withdraw procedure
withdraw(500); -- Valid withdrawal
withdraw(2000); -- Invalid withdrawal (insufficient funds)
withdraw(-100); -- Invalid withdrawal (negative amount)

END;

Dated signature
Marks Obtained
of Teacher

Process Product
Total (25)
Related (10) Related (15)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy