PostgreSQL 异常
PostgreSQL 异常 摘要:在本教程中,您将学习如何在 PL/pgSQL 中捕获 PostgreSQL 异常。
PL/pgSQL 异常子句介绍 当块中发生错误时,PostgreSQL 将中止块的执行以及周围的事务。
要从错误中恢复,您可以使用块中的exception子句begin…end。
下面说明了该exception子句的语法:
«label» declare begin statements; exception when condition [or condition…] then handle_exception; [when condition [or condition…] then handle_exception;] [when others then handle_other_exceptions; ] end;
这个怎么运作。
首先,当begin和之间发生错误时exception,PL/pgSQL 停止执行并将控制权交给异常列表。 其次,PL/pgSQL 搜索第一个condition匹配发生的错误。 第三,如果匹配,handle_exception则执行相应的语句。PL/pgSQL 将控制权交给end关键字后的语句。 最后,如果没有找到匹配项,错误就会传播出去并且可以被exception封闭块的子句捕获。如果没有包含该exception子句的封闭块,PL/pgSQL 将中止处理。 条件名称可以是no_data_found在的情况下,select声明不返回行或者too_many_rows如果select语句返回多行。有关PostgreSQL 网站上的条件名称的完整列表。
也可以通过SQLSTATE代码指定错误条件。例如,P0002forno_data_found和P0003for too_many_rows。
通常,您将捕获特定的异常并相应地处理它。要处理其他异常而不是您在列表中指定的异常,您可以使用该when others then子句。
处理异常示例 我们将使用示例数据库中的film表进行演示。
电影台
- 处理 no_data_found 异常示例 以下示例会发出错误,因为 ID 为 2000 的电影不存在。
do $$ declare rec record; v_film_id int = 2000; begin – select a film select film_id, title into strict rec from film where film_id = v_film_id; end; $$ language plpgsql;
输出:
ERROR: query returned no rows CONTEXT: PL/pgSQL function inline_code_block line 6 at SQL statement SQL state: P0002
以下示例使用exception子句捕获no_data_found异常并报告更有意义的消息:
do $$ declare rec record; v_film_id int = 2000; begin – select a film select film_id, title into strict rec from film where film_id = v_film_id; – catch exception exception when no_data_found then raise exception ‘film % not found’, v_film_id; end; $$ language plpgsql;
输出:
ERROR: film 2000 not found CONTEXT: PL/pgSQL function inline_code_block line 14 at RAISE SQL state: P0001
2)处理too_many_rows异常示例 以下示例说明了如何处理too_many_rows异常:
do $$ declare rec record; begin – select film select film_id, title into strict rec from film where title LIKE ‘A%’;
exception
when too_many_rows then
raise exception 'Search query returns too many rows';
end; $$ language plpgsql;
输出:
ERROR: Search query returns too many rows CONTEXT: PL/pgSQL function inline_code_block line 15 at RAISE SQL state: P0001
在此示例中,too_many_rows发生异常是因为该select into语句返回多于一行,而本应返回一行。
- 处理多个异常 以下示例说明了如何捕获多个异常:
do $$ declare rec record; v_length int = 90; begin – select a film select film_id, title into strict rec from film where length = v_length;
-- catch exception
exception
when sqlstate 'P0002' then
raise exception 'film with length % not found', v_length;
when sqlstate 'P0003' then
raise exception 'The with length % is not unique', v_length;
end; $$ language plpgsql;
输出:
ERROR: The with length 90 is not unique CONTEXT: PL/pgSQL function inline_code_block line 17 at RAISE SQL state: P0001
- 将异常作为 SQLSTATE 代码处理 下面的示例与上面的示例相同,只是它使用SQLSTATE代码而不是条件名称:
do $$ declare rec record; v_length int = 30; begin – select a film select film_id, title into strict rec from film where length = v_length;
-- catch exception
exception
when sqlstate 'P0002' then
raise exception 'film with length % not found', v_length;
when sqlstate 'P0003' then
raise exception 'The with length % is not unique', v_length;
end; $$ language plpgsql;
输出:
ERROR: film with length 30 not found CONTEXT: PL/pgSQL function inline_code_block line 15 at RAISE SQL state: P0001
概括 使用块中的exception子句begin…end来捕获和处理异常。