DECLARE
pe_ratio NUMBER
(3,1
);
BEGIN
SELECT price / earnings INTO pe_ratio
FROM stocks
WHERE symbol =
'XYZ'; -- might cause division-by-zero error
INSERT INTO stats
(symbol, ratio
) VALUES
('XYZ', pe_ratio
);
COMMIT;
EXCEPTION -- Exception handler
WHEN ZERO_DIVIDE THEN -- handles
'division by zero' error
INSERT INTO stats
(symbol, ratio
) VALUES
('XYZ', NULL
);
COMMIT;
...
WHEN OTHERS THEN -- handles all other errors
ROLLBACK;
dbms_output.put_line
(SubStr
('Error '||TO_CHAR
(SQLCODE
)||
': '||SQLERRM, 1, 255
));
END; -- exception handlers
and block end here