Firebird-处理异常的自定义消息 [英] Firebird - handling exception's custom-message

查看:63
本文介绍了Firebird-处理异常的自定义消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebird 2.5.我正在尝试在存储过程中处理用户定义的异常自定义消息.

I am using Firebird 2.5. I'm trying to handle user defined exception custom message in a stored procedure.

我有两个过程.第一个引发带有附加信息的异常(自定义异常消息).第二个从第一个选择数据,它正在尝试处理异常.

I have two procedures. The first one raise exception with additional information (custom exception message). The second one selects data from the first one and it is trying to handle the exception.

我在读取异常的自定义消息时遇到问题.这是显示和测试问题的完整脚本.

I have problem with reading the custom message of the exception. Here is complete script to show and test the problem.

create exception ex_empty '';   

create procedure the_first_one (iid integer)
returns (text varchar(50))
as
begin
  text = '';
  if (:iid <= 0) then
    exception ex_empty 'ID must be positive';
  else if (:iid >= 100) then
    exception ex_empty 'ID is too big';
  else
    text = 'OK';

  suspend;
end;

create procedure the_second_one
returns (text2 varchar(50))
as
begin
  select the_first_one.text from the_first_one(1) into :text2;
  suspend;
  select the_first_one.text from the_first_one(-1) into :text2;
  suspend;
  select the_first_one.text from the_first_one(200) into :text2;
  suspend;

  when exception ex_empty do
  begin
    text2 = 'Bad input parameter, the '; /* here I want to add ... || :the_firs_one_exception_custom_message;  */
    suspend;
  end
end;

现在开始第二个过程,我可以看到结果:

When I start now the_second_one procedure I can see results:

OK
Bad input parameter, the

是否可以在 WHEN EXCEPTION 块中处理自定义的异常消息?我想看这个:

Is it possible to handle the custom message of exception in the WHEN EXCEPTION block? I want to see this:

OK
Bad input parameter, the ID must be positive

我知道解决方案,不要在the_first_one过程中引发异常,而是返回错误消息作为结果.但是,如果我已经在很多地方使用了该过程,那么我就不想更改该机制.

I know the solution, to not raise exception in the_first_one procedure, but instead return the error message as a result. But if I have already used that procedure in quite many places, I don't want to change the mechanism.

在现实世界中,the_first_one将一些文档插入数据库,并且受到很大限制.现在,我想从外部来源导入许多文档,并且以非例外的方式通知用户我们的数据有问题.

In real world the_first_one inserts to database some documents, and is quite restricted. Now I want to import many documents from external source and in non-exception way inform the user, that we had some problem with data.

推荐答案

目前无法实现.异常消息在PSQL上下文中不可用.

This is currently not possible. The exception messages are not available in the PSQL context.

有一个改进请求,需要添加此功能: CORE-2382:添加了获取类型的可能性并将自定义异常的消息放入变量.这已针对Firebird 4实施,预计将于2021年发布.

There is an improvement request to add this feature: CORE-2382: Add possibility to get type and message of custom exception into variable. This has been implemented for Firebird 4, which is expected to be released in 2021.

您有两种选择:

  1. 如前所述,要将异常消息作为返回参数传递,
  2. 处理调用存储过程的客户端中的错误.

这篇关于Firebird-处理异常的自定义消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆