Using PL/SQL exception handling in a procedure
In object-oriented languages, we have the concept of exception blocks and catch blocks. The code in the exception block can raise an exception and the catch block handles this exception. We can implement a similar logic in DB2 by using exception blocks.
As part of PL/SQL support, DB2 9.7 extends the support for exception handling by providing the concept of exception blocks. We can define a set of possible exceptions and their respective handlers for a given set of SQL statements. In this recipe, we will discuss how we can design and implement exception handling by using PL/SQL exception blocks.
How to do it...
To implement PL/SQL exception handling, we need to declare all exceptions that we anticipate and want to handle. Then we need to provide the handlers for each type of exception. This complete task follows the following syntax:
DECLARE declarations
BEGIN SQL statements
EXCEPTION HANDLERS
END
Let’s discuss how to implement these sections...