RDBMS PR 16
RDBMS PR 16
RDBMS PR 16
PRACTICAL: 16
Aim: Implement PL/SQL programs using exception handling.
Exception handling
An exception is an error which disrupts the normal flow of program instructions.
An error occurs during the program execution is called Exception in PL/SQL.
PL/SQL provides us the exception block which raises the exception thus helping
theprogrammer to find out the fault and resolve it.
These exceptions are predefined in PL/SQL which get raised WHEN certain
database rule is violated.
Named system exceptions: They have a predefined name by the system like
ACCESS_INTO_NULL, DUP_VAL_ON_INDEX, LOGIN_DENIED etc. the list is quite big.
DECLARE
c_id customer22090.id%type := 8;
c_name customer22090.name%type;
c_addr customer22090.address%type;
BEGIN
SELECT name, address INTO c_name, c_addr FROM customer22090 WHERE id = c_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
EXCEPTION
WHEN no_data_found THEN dbms_output.put_line('No such customer!');
WHEN others THEN dbms_output.put_line('Error!');
END;
/
OUTPUT
After the execution of above code at SQL Prompt, it produces the following result:
The above program should show the name and address of a customer as result whose ID is
given. But there is no customer with ID value 8 in our database, so the program raises the run-time
exception NO_DATA_FOUND, which is captured in EXCEPTION block.
If you use the id defined in the above table (i.e. 7090 to 7079), you will get a
certain result. For a demo example: here, we are using the id 7090.
DECLARE
c_id customer22090.id%type := 7090;
c_name customer22090.name%type;
c_addr customer22090.address%type;
BEGIN
SELECT name, address INTO c_name, c_addr
FROM customer22090
WHERE id = c_id;
DBMS_OUTPUT.PUT_LINE ('Name: '|| c_name);
DBMS_OUTPUT.PUT_LINE ('Address: ' || c_addr);
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('No such customer!');
WHEN others THEN
dbms_output.put_line('Error!');
END;
/
OUTPUT:
After the execution of above code at SQL prompt, you will get the following result:
CONCLUSION:
Sign : Date: