CREATE TABLE AREAS
(
Radius number (5 ),
Area number (14,2)
);
Method 1: Use goto
DECLARE
Pi constant number (9, seven): = 3.1415927;
Radius INTEGER (5 );
Area NUMBER (14, 2 );
BEGIN
Radius: = 0;
LOOP
<Here>
Radius: = radius + 1;
IF radius = 4
THEN
GOTO here;
ELSE
Area: = pi * POWER (radius, 2 );
Insert into areas
VALUES (radius, area );
Exit when area> 100;
End if;
End loop;
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
ROLLBACK;
Raise;
End;
-- Method 2: Use exception
-- System exception
Declare
Pi constant number (9, seven): = 3.1415927;
Radius INTEGER (5 );
Area Number (14, 2 );
Some_variable number (14, 2 );
Begin
RADIUS: = 0;
Loop
Begin
RADIUS: = radius + 1;
Some_variable: = 1/(radius-4 );
Area: = pi * POWER (radius, 2 );
Insert into areas
VALUES (radius, area );
Exit when area> 100;
EXCEPTION
WHEN ZERO_DIVIDE
THEN
NULL;
END;
End loop;
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
ROLLBACK;
RAISE;
END;
-- Custom exception
DECLARE
Pi constant number (9, seven): = 3.1415927;
Radius INTEGER (5 );
Area NUMBER (14, 2 );
E_userdefinedexception exception;
Pragma exception_init (e_userdefinedexception,-1401 );
Begin
RADIUS: = 0;
Loop
RADIUS: = radius + 1;
Begin
If radius = 4
Then
Raise e_userdefinedexception;
Else
Area: = pI * power (radius, 2 );
Insert into areas
VALUES (radius, area );
Exit when area> 100;
End if;
EXCEPTION
WHEN e_userdefinedexception
THEN
NULL;
END;
End loop;
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
ROLLBACK;
RAISE;
END;