Lab 7.1 Exercises

7.1.1 Understanding the Importance of Error Handling

In this exercise, you will calculate the value of the square root of a number and display it on the screen.

Create the following PL/SQL script:

 -- ch07_1a.sql, version 1.0 SET SERVEROUTPUT ON; DECLARE    v_num NUMBER := &sv_num; BEGIN    DBMS_OUTPUT.PUT_LINE ('Square root of 'v_num       ' is 'SQRT(v_num)); EXCEPTION    WHEN VALUE_ERROR THEN       DBMS_OUTPUT.PUT_LINE ('An error has occurred'); END; 

In the preceding script, the exception VALUE_ERROR, is raised when conversion or type mismatch errors occur. This exception is covered in greater detail in Lab 7.2 of this chapter. In order to test this script fully, execute it two times. For the first run, enter a value of 4 for the variable v_num . For the second run, enter the value of -4 for the variable v_num . Execute the script, and then answer the following questions:


What output was printed on the screen (for both runs)?


Why do you think an error message was generated when the script was run a second time?


Assume that you are not familiar with the exception VALUE_ERROR. How would you change this script to avoid this runtime error?

Oracle PL[s]SQL by Example
Oracle PL[s]SQL by Example
ISBN: 3642256902
Year: 2003
Pages: 289 © 2008-2017.
If you may any questions please contact us: