When an exception occurs, you can identify the associated error code or error
message by using the following two functions:
Now that you have learned all about exception handlers, you can build a PL/SQL block that requires you to handle exceptions.
In the following simulation, you will work with a preexisting PL/SQL block. The current PL/SQL block processes orders and updates inventory. Inventory includes pet supplies for animals. Customers can purchase any item individually. The shipping and handling fee for every transaction is 0.10% of the sale cost, and there is a 5% state tax.
The PL/SQL block currently does the following:
- Accepts the product ID
- Gets the base sale price from the product table (based on the product ID)
- Calculates the total sale amount as the sum of base sale price, tax amount, and shipping and handling costs
- Displays the product sales price on the screen
To make this code more robust, you need to add an error handling routine. You need to catch the exceptions for the following conditions:
- If there is no product with the product ID supplied by the end user, raise the appropriate error.
- If the base sale price is less than or equal to zero, raise an error.
- Trap all other database errors that may arise in the PL/SQL block.