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

PLSQL 7 1 SG

This document provides an overview of handling errors in PL/SQL programs. It discusses the importance of anticipating and handling errors to avoid programs abruptly ending. Examples of common errors that may occur are given, as well as how to write exception handling code to gracefully handle errors. The document also covers predefined versus user-defined exceptions and stresses the importance of leaving the database in a consistent state when errors happen.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

PLSQL 7 1 SG

This document provides an overview of handling errors in PL/SQL programs. It discusses the importance of anticipating and handling errors to avoid programs abruptly ending. Examples of common errors that may occur are given, as well as how to write exception handling code to gracefully handle errors. The document also covers predefined versus user-defined exceptions and stresses the importance of leaving the database in a consistent state when errors happen.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Release Date: August, 2015

Updates:

1
2
3
You are going to learn how to handle errors so that an abrupt end of program does not occur. Programs
should be managed so that even unanticipated errors are handled, no program should ever just ‘bomb’ out
with an error.

4
Ask the students to discuss errors that they may have seen in an application. The error can be a handled or
unhandled error. Below are some examples:
– Entering an incorrect username and/or password
– Forgetting to include the @ in an email address
– Entering a credit card number incorrectly
– Entering an expiration date that has passed
– Selecting more than one row into a single variable
– Receiving “no rows returned” from a select statement

5
To confirm the spelling of Republic of Korea in the data, run a statement such as the following:

SELECT country_name
FROM wf_countries
WHERE country_name like '%Korea%';

6
7
Remind students that a SELECT statement in PL/SQL must return exactly one row. This statement returns
no rows and therefore raises an exception in the executable section.

8
Exception handling is code written to handle errors that occurs during execution. PL/SQL programs will
start to get longer and more complicated due to the exception handling code needed to handle all errors,
but that is preferable to programs just ‘bombing’ out with unhandled exceptions.

9
These are the more frequent types of errors that may occur during the execution of a program. There are
many errors and all should be handled for a PL/SQL program to run smoothly and efficiently.

Point out that one error can often cause another. For example, employee_id 100 already exists in the
EMPLOYEES table. A user tries to add a new employee and mistakenly types an employee id of 100. The
program then tries to INSERT this new employee and violates a primary key constraint. This is an example
of a user action errors causing a data error.

10
11
Programs are written to support or enhance the existing systems. If unhandled errors occur and the
program execution is stopped, users are frequently confused and might eventually be angry and frustrated
with the systems they are trying to use. So, errors need to be handled within the code to keep the
execution of the program running continuously.

12
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.

13
Explain the terms used here. When an error occurs, we say that an exception has been raised. A handler is
a specific WHEN clause in the EXCEPTION section.

14
15
Stress that when an exception is raised, the rest of the executable section of the block is NOT executed;
instead, the EXCEPTION section is searched for a suitable handler.

16
There is no exception handler in this block, therefore the block terminates unsuccessfully, returning an
‘unhandled exception’ status code to the calling environment (Application Express), which then reports the
exception as shown.

17
In this case we have successfully handled the exception inside the block, so PL/SQL returns a ‘success’
status code to the calling environment, which therefore will report ‘Statement Processed’.

18
19
20
There are two types of exceptions in PL/SQL. “Predefined exception” and “User-defined exception”.
Predefined exceptions are ones that are built into the language, where Oracle has already associated a
known Oracle error number with a named exception. Often used predefined exceptions are
TOO_MANY_ROWS and NO_DATA_FOUND, and you can most like guess what they do and when they are
raised, just from their name. User-defined exceptions are created by the programmer in the DECLARE
section, raised by the programmer in the executable section and handled by the programmer in the
exception section. Students will learn more about this in the next two lessons.

21
22
23
24
25
26
27
No matter how severe the error is, you want to leave the database in a consistent state and avoid storing
any bad data, so you do need to pay special care and attention to when your program issues commit or
rollback statements.

28
Exception – Occurs when an error is discovered during the execution of a program that disrupts the normal
operation of the program.
Exception Handler – Code that defines the recovery actions to be performed when execution-time errors
occur.

29
30
31

You might also like