0% found this document useful (0 votes)
30 views15 pages

Handling Exceptions-Lectureslides

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)
30 views15 pages

Handling Exceptions-Lectureslides

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

•Describe several advantages of including exception handling

code in PL/SQL
•Describe the purpose of an EXCEPTION section in a PL/SQL block
•Create PL/SQL code to include an EXCEPTION section
•List several guidelines for exception handling
What is an Exception?
An exception occurs when an error is discovered during the
execution of a program that disrupts the normal operation of the
program.
There are many possible causes of exceptions: a user makes a
spelling mistake while typing; a program does not work correctly;
an advertised web page does not exist; and so on.
Can you think of errors that you have come across while using a
web site or application?
Exceptions in PL/SQL
This example works fine. But what if you entered ‘Korea, South’
instead of ‘Republic of Korea’?
Exceptions in PL/SQL (continued)

The code does not work as expected. No data was found for Korea, South
because the country name is actually stored as Republic of Korea.
This type of error in PL/SQL is called an exception. When code does not
work as expected, PL/SQL raises an exception. When an exception is
raised, the rest of the execution section of the PL/SQL block is not
executed.
What Is an Exception Handler?
An exception handler is code that defines the recovery actions to
be performed when an exception is raised (that is, when an error
occurs).
When writing code, programmers need to expect the types of
errors that can occur during the execution of that code. They need
to include exception handlers in their code to address these errors.
Exception handlers allow programmers to "bulletproof" their code.
What types of errors might programmers want to account for by
using an exception handler?
•System errors (for example, a hard disk is full)
•Data errors (for example, trying to duplicate a primary key
value)
•User action errors (for example, data entry error)
•Many other possibilities!
Why is Exception Handling Important?
• Protects the user from errors (Frequent errors can frustrate the
user and/or cause the user to quit the application.)

• Protects the database from errors (Data can be lost or


overwritten.)

• Major errors take a lot of system resources (If a mistake is


made, correcting the mistake can be costly; users might
frequently call the help desk for assistance with errors.)

• Code is more readable (Error-handling routines can be written


in the same block in which the error occurred.)
Handling Exceptions with PL/SQL – Example 1
A block always terminates when PL/SQL raises an exception, but
you can specify an exception handler to perform final actions
before the block ends. The exception section begins with the
keyword EXCEPTION.
Handling Exceptions with PL/SQL- Example 1 (continued)
When an exception is handled, the PL/SQL program does not
terminate abruptly.
When the exception is raised, the control shifts to the exception
section and the handler in the exception section is executed. The
PL/SQL block terminates with normal, successful completion.

Only one exception can occur at a time. When an exception occurs,


PL/SQL processes only one handler before leaving the block.
Handling Exceptions with PL/SQL – Example 2
The select statement in the block is retrieving the last_name of
Stock Clerks.

However, an exception is raised because more than one


ST_CLERK exists in the data.
Handling Exceptions with PL/SQL – Example 2 (continued)
The following code includes a handler for a predefined Oracle server
error called TOO_MANY_ROWS.
The OTHERS Exception Handler

Neither Data not found exception nor too many rows exception are
occurred,
if some other exception is raised, then the statements in the OTHERS
exception handler are executed.
• Exception
• Exception handler
• Describe several advantages of including exception handling code
in PL/SQL

• Describe the purpose of an EXCEPTION section in a PL/SQL block

• Create PL/SQL code to include an EXCEPTION section


1. What happens when Oracle encounters a runtime problem while executing a
PL/SQL block?
2. What do you need to add to your PL/SQL block to address these problems?
3. List three advantages of handling exceptions within a PL/SQL block.
4. Run this PL/SQL code and then answer the questions that follow.
DECLARE
v_jobid employees.job_id%TYPE;
BEGIN
SELECT job_id
INTO v_jobid
FROM employees
WHERE department_id = 80;
END;
What happens when you run the block?

You might also like