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.

29 juni 2026

Welke midlance regeling past bij mij?

Medium-lance / Midlance wint snel aan populariteit. Het combineert de zekerheid van een vast dienstverband met de mogelijkheid om een aanzienlijk hoger inkomen te verdienen wanneer je op mooie opdrachten wordt ingezet.

18 juni 2026

AI inspiratiesessie

AI is nauwelijks nog weg te denken uit het nieuws. Vrijwel dagelijks verschijnen er berichten over nieuwe AI-toepassingen. Dat roept vragen op. Wat is AI eigenlijk precies? Wat kunnen deze systemen wel en niet? Wat betekent dit voor mij?

Opdrachten uitgevoerd voor

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