PostgreSQL自定义异常条件

编程入门 行业动态 更新时间:2024-10-24 02:00:07
本文介绍了PostgreSQL自定义异常条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

引发异常时是否可以创建自定义条件? 考虑以下示例:

Is it possible to create custom conditions when I raise an exception? Consider the following example:

BEGIN y := x / 0; EXCEPTION WHEN division_by_zero THEN RAISE NOTICE 'caught division_by_zero'; RETURN x; END;

在这里,我使用 division_by_zero条件来捕获异常。 我想做的是这样的:

Here I use 'division_by_zero' condition to catch the exception. What I'd like to do is something like this:

BEGIN [...] RAISE custom_condition; EXCEPTION WHEN custom_condition THEN [...] END;

这样我就不会干扰可能的标准例外情况。我可以做y:= 1/0;并捕获divide_by_zero,但看起来不正确。

so that I don't interfere with possible standard exceptions. I could just do y:= 1 / 0; and catch division_by_zero, but it does not look right.

推荐答案

begin if $1='bar' then raise exception using errcode='NOBAR', message='Bar is prohibited', hint='We do not talk to this guy'; end if; exception when sqlstate 'NOBAR' then update nobar_raised set count=count+1; end;

更多信息:

  • 错误和消息

诱捕错误

更多推荐

PostgreSQL自定义异常条件

本文发布于:2023-07-21 11:11:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1176929.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   异常   条件   PostgreSQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!