13th Example - EXCEPTION on PL/SQL



In this example will be shown the error handling on PL/SQL block in case we have made ​​an error in logic. Error handling in PL/SQL block using one operator EXCEPTION. Here is an example of the use of EXCEPTION operators to handle logic errors division number 2 values :

DECLARE
 a NUMBER;
 b NUMBER;
 hasil NUMBER;
BEGIN
 a := &inputa;
 b := &inputb;
 hasil := a / b;
 DBMS_OUTPUT.PUT_LINE (hasil);
 EXCEPTION
  WHEN ZERO_DIVIDE THEN
        DBMS_OUTPUT.PUT_LINE ('You have an Error on your formula !');
END;
/

Above PL/SQL block if executed would be fine and displays the results in the form of the distribution of the value of a shared b. But if the value of b is 0 (zero), there will be an error found. If this happens, EXCEPTION will take over and display the message "You have an Error on your formula". EXCEPTION operates by identifying the error that occurred and match the WHEN condition ZERO_DIVIDE.

No comments

Powered by Blogger.