Handling EXCEPTION and return result from function

拈花ヽ惹草 提交于 2019-12-05 22:58:13

The EXCEPTION clause needs to be in the same block as the exception.

For instance:

CREATE OR REPLACE FUNCTION test_excep (arg integer)
  RETURNS integer
AS
$func$
DECLARE
   res INTEGER;
BEGIN

res := 100 / arg;

RETURN res;

EXCEPTION
    WHEN division_by_zero 
    THEN  RETURN 999;

END
$func$
LANGUAGE plpgsql;
Igor Romanchenko

Here is a correct function:

CREATE OR REPLACE FUNCTION test_excep (arg INTEGER) RETURNS INTEGER 
AS $$
    DECLARE res INTEGER;
    BEGIN
        res := 100 / arg;
        RETURN res;
    EXCEPTION
        WHEN division_by_zero 
        THEN  RETURN 999;
    END;
$$
LANGUAGE plpgsql;

EXCEPTION part must be inside the block where the exception is, and also must be the last part of the block.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!