RSS

7.1 Handling Errors

09 Apr

Lab Objectives

After completing this lab, you will be able to

  • Understand the importance of error handling

PL/SQL has two types of exceptions: built-in and user-defined.

The following example illustrates some of the differences between compilation and runtime errors:

DECLARE

    v_num1  INTEGER := &sv_num1;

    v_num2  INTEGER := &sv_num2;

    v_result    NUMBER;

BEGIN

    v_result = v_num1 / v_num2; –IF v_num2 = 0 Runtime Error

    DBMS_OUTPUT.PUT_LINE(‘v_result: ‘ || v_result);

END;

 

To handle this type of error in the program, you must add an exception handler. The exception-handling section has the following structure:

EXCEPTION

WHEN EXCEPTION_NAME THEN

ERROR-PROCESSING STATEMENTS;

 

The exception-handling section is placed after the executable section of the block. The preceding example can be rewritten in the following manner:

DECLARE

    v_num1 INTEGER := &sv_num1;

    v_num2 INTEGER := &sv_num2;

    v_result NUMBER;

BEGIN

    v_result := v_num1 / v_num2;

    DBMS_OUTPUT.PUT_LINE (‘v_result: ‘||v_result);

    EXCEPTION

        WHEN ZERO_DIVIDED THEN

            DBMS_OUTPUT.PUT_LINE(‘A number cannot be divided by zero.’);

END;

Advertisements
 
Leave a comment

Posted by on 04/09/2010 in ORACLE

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: