Error handling with PL/SQL

oracle development (author: Joost Drost)

inSystems Error

What went wrong? Where did it go wrong, and why? These are the questions that first rise in mind when an error occurs. Error handling is the part of the programming that must deal with this. And although it cannot answer these questions directly, it can gather the information that will help in providing an answer.

The information can be divided into three categories (what, where and why):

  • The what is about the error itself. What kind of error occurred, what is the error message.
  • The where is about the program units involved in raising the error.
  • The why is about the data that was processed when the error occurred.

Gathering this information is one of the primary goals of error handling.

Solution Design

Now that the objective is known, we need to create a design that will meet these objectives.

The data will be kept in a collection. We use encapsulation to hide the actual variable from the “outside world”. There are stored procedures that give controled access to the variable: add_detail and clear_details. Using a collection also allows us to keep data just in case of an error. When there is no error, it can be trown away. This is also usefull when there is more than 1 exception handler and each exception must store the same information.

De data will be stored in 2 tables, a master table “LOGS” and a detail table “LOG_DETAILS”. This will provide an easy insight in the number of errors: one row in table LOGS for each error and any number of rows in LOG_DETAILS. One procedure is doing the task of storing both the log and the log_details.

The datatype of the value that is stored is varchar2 so that any datatype will fit in. Additionally an attribute NAME can be stored, to form a name value pair with the VALUE. So you can store the column name together with the value at the time of the error. The program_unit attribute can hold the name of the procedure where the error occurs.
The LOGS table is generic and can also hold other types of log entries besides errors.

Example 1

The example 1 demo program loops through the table EMP and adds 10 to the salary of each employee. This is done by calling a procedure from package emp_demo. When the empno ends with 00, an error is caused by division by zero. Only one row in EMP meets this condition so we expect only one error.

Output in Table LOGS

inSystems log tables

Discussion

  1. When you review the demo package and the example scripts you may notice that the exception handlers contain only 3 different statements or actions: add_detail, raise and save. In a real world environment there will be other requirements as well, for example send a mail. These additional actions should be described in a design document. It is not the responsiblity of a developer to define what actions are appropriate. The complete list of actions to be considered for any exception handler should be in the design document. A definition of error handling from a developers point of view is: for each error select and apply the appropriate actions.
  2. How many exception handlers do you need then? The answer is: one for each unique combination of actions. As demonstrated in example 2 there is no need to define an exception handler for each different error. When the actions are the same the error can be handled by the same exception handler.
  3. The process of defining your error handlers can be simplified by distinguishing primary and secundary exception handlers. Secundary ones are those like in the error_handling_demo package: procedures that are never called directly but always by another procedure. The exception handler can be standard: add_detail(s) and reraise the error.
  4. The rest defines the primary category. This is where the real choices have to be made.
    One of the additional benefits from this method of error handling, is that it works great for debugging. When applied directly during the development process, it will help you find the bugs more quickly.

Share this

Contact

Heb je vragen of opmerkingen? Aarzel niet om contact op te nemen. Dat kan via de algemene contactgegevens, rechtstreeks met de genoemde contactpersoon op een van de detailpagina’s, of via onderstaande knop.

31 maart 2026

APEX World 2026 – ’n impressie

inSystems was op de “Sweet 16″ editie van APEX World als sponsor vertegenwoordigd middels verschillende banners en met 5 deelnemers. Tezamen hebben zij veel interessante keynote sessies en parallelsessies bijgewoond.

2 april 2026

30 jaar inSystems in vogelvlucht

inSystems viert haar 30-jarig bestaan. Wat ooit begon als Quobell B.V., opgericht in 1996 door vijf ervaren IT-professionals met Oracle als gemeenschappelijke factor, is uitgegroeid tot een stabiele en herkenbare speler in de Nederlandse IT-markt.

Opdrachten uitgevoerd voor

RIGD LOXIA
Holland Casino
Stedin
Bank Mendes Gans
WVDB Adviseurs Accountants
DICTU
CBR
Allianz
Alphabet
Fokker
RAVU
VTTI
Telegraaf Media Groep
UBR Uitvoeringsorganisatie Bedrijfsvoering Rijk
BMW
AgroEnergy
MN Services
Leaseplan
CB Logistics
Rendo
Van Iperen Groeispecialisten
SVB
Thales
Gemeente Apeldoorn
Politie
Oracle University
Stratech Social
Provincie Utrecht
Syntrus Achmea Real Estate & Finance
Rechtspraak
Darling
Arval BNP Paribas
ASR Nederland
Feenstra inSystems
VAA ICT Consultancy
Multi Tank Card
JUVA
inSystems KLM
IBM
NVWA inSystems
Ahold
Rijk Zwaan inSystems
Gemeente Den Haag
Ziggo
Gemeente Gouda
VION Food Group
TenneT
Ministerie van Economische Zaken, Landbouw & Innovatie
TU Eindhoven
Nederlandse Spoorwegen
Cannock
Universiteit Tilburg
Rabobank
Altran
Greenchoice
PC Uitvaart
Eneco
inSystems Unive
ING Bank
GVB