In order to define an exception inside the body of the loop, you must define the PL/SQL block inside the body of the loop. Therefore, when an exception is raised, it will terminate the block, and the control will be transferred to the first executable statement after END. For example,
FOR i IN 1..3 LOOP BEGIN SELECT first_name INTO v_first_name FROM student WHERE student_id = 123; EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE ('Error'); END; END LOOP;
In this case, if there is no student corresponding to student ID 123, the exception NO_DATA_FOUND is raised. This causes the PL/SQL block to terminate. However, as long as the value of the loop counter ranges between lower and upper limits, the PL/SQL block will be executed repeatedly. |
No comments:
Post a Comment