You are on page 1of 8

Lab3.

Handling exceptions

1. Add at least three exception handling sections that trap all three different
types of exceptions (pre-defined, non-predefined, user-defined).

2) non predefined
DECLARE
v_movie_id VARCHAR2(20) := 'string';
v_found_movie movie%ROWTYPE;
BEGIN
BEGIN
v_movie_id := TO_CHAR(TO_NUMBER(v_movie_id));
EXCEPTION
WHEN OTHERS THEN
RAISE_APPLICATION_ERROR(-20001, 'Invalid movie id: ' || v_movie_id);
END;
SELECT * INTO v_found_movie FROM movie WHERE movie_id =
v_movie_id;

DBMS_OUTPUT.PUT_LINE('Found movie with ID ' ||


v_found_movie.movie_id || ': ' || v_found_movie.title);

EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
1) pre-defined
DECLARE
v_movie_count NUMBER;
BEGIN
SELECT COUNT(*) INTO v_movie_count FROM movie WHERE release_date
= '02-09';

DBMS_OUTPUT.PUT_LINE('There are ' || v_movie_count || ' movies released in


2019.');

EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;

3) user defined
DECLARE
v_movie_name VARCHAR2(100) := 'Star Wars: Episode VII - The Force
Awakens';
some_variable movie%Rowtype;--VARCHAR2(100);
BEGIN
SELECT * INTO some_variable FROM movie WHERE title = v_movie_name;
IF SQL%ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20002, 'Movie not found: ' ||
v_movie_name);
END IF;
DBMS_OUTPUT.PUT_LINE('Found the movie: ' || v_movie_name);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;

2. Try incorporating exception handling into the anonymous blocks you created
in lab 2.

DECLARE
runt movie.runtime%TYPE := 149;
bud movie.budget%TYPE := 50000;
BEGIN
FOR movie_table IN (SELECT * FROM movie WHERE runt = runtime AND
budget > bud)
LOOP
BEGIN
DBMS_OUTPUT.PUT_LINE(movie_table.title || ' spent ' ||
movie_table.budget);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
END LOOP;
END;

DECLARE
mov_id NUMBER := 5;
name movie.title%TYPE;
bud movie.budget%TYPE;
movie_id movie.movie_id%TYPE;

CURSOR mov_cursor IS
SELECT title, budget, release_date
FROM movie
WHERE movie_id = mov_id;
BEGIN
FOR mov_table IN mov_cursor LOOP
BEGIN
name := mov_table.title;
bud := mov_table.budget;

IF bud > 50000 THEN


DBMS_OUTPUT.PUT_LINE(name || ' is an expensive movie');
ELSE
DBMS_OUTPUT.PUT_LINE(name || ' is not an expensive movie');
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
END LOOP;
END;
DECLARE
TYPE gen_table IS TABLE OF genre.genre_name%TYPE INDEX BY
BINARY_INTEGER;
gen_names gen_table;
gen_id genre.genre_id%TYPE := 1002;
BEGIN
FOR i IN 1..10 LOOP
BEGIN
SELECT genre_name
INTO gen_names(i)
FROM genre
WHERE genre_id = gen_id;

gen_id := gen_id + 1;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for genre_id ' || gen_id);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;
END LOOP;

FOR i IN 1..gen_names.COUNT LOOP


DBMS_OUTPUT.PUT_LINE(gen_names(i));
END LOOP;
END;
DECLARE
TYPE movie_info IS RECORD (
mov_id movie.movie_id%TYPE,
mov_name movie.title%TYPE,
bud movie.budget%TYPE
);
v_mov_id movie.movie_id%TYPE := '285';
v_movie movie_info;

BEGIN
BEGIN
SELECT movie_id, title, budget
INTO v_movie
FROM movie
WHERE movie_id = v_mov_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('No data found for movie_id ' || v_mov_id);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('An error occurred: ' || SQLERRM);
END;

DBMS_OUTPUT.PUT_LINE('Movie ID: ' || v_movie.mov_id);


DBMS_OUTPUT.PUT_LINE('Name: ' || v_movie.mov_name);
DBMS_OUTPUT.PUT_LINE('Budget: ' || v_movie.bud);
END;

Questions:
1. What is an exception?
An exception is an error that occurs during the execution of a PL/SQL program. Exceptions can
be caused by various factors such as user input errors, system errors, or program logic errors.
2. What types of exceptions exist in PL/SQL?
3: pre-defined, non-defined, user-defined
3. How are exceptions handled?
By using exception handlers in PL/SQL code. An exception handler is a block of code that is
executed when an exception occurs.
4. Describe the structure of the exception handling section.
The structure of an exception handling section consists of the EXCEPTION keyword followed
by one or more WHEN clauses. Each WHEN clause specifies a particular exception and the code
to be executed when that exception is raised. An optional OTHERS clause can be used to specify
code to be executed when an exception that is not explicitly handled occurs.
5. What are predefined Oracle server errors and how are they handled?
Predefined Oracle server errors are a set of errors that are predefined by Oracle. These errors are
raised when certain predefined conditions are met, such as NO_DATA_FOUND,
TOO_MANY_ROWS, and VALUE_ERROR. These errors can be handled using the
EXCEPTION block in PL/SQL code.
6. What are non-predefined Oracle server errors and how are they handled?
Non-predefined Oracle server errors are errors that are not predefined by Oracle. These errors
can occur due to issues such as database connection problems, network failures, or hardware
failures. These errors can be handled by using the EXCEPTION block in PL/SQL code and
implementing appropriate error-handling strategies.
7. What are user-defined exceptions and how are they handled?
User-defined exceptions are exceptions that are defined by the developer in PL/SQL code. These
exceptions can be raised explicitly using the RAISE statement and can be handled by using the
EXCEPTION block in PL/SQL code. User-defined exceptions allow developers to create custom
error-handling strategies for their applications.

You might also like