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

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

问题描述

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



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



我有读取异常的自定义消息的问题。这是完整的脚本来显示和测试问题。

 创建异常ex_empty''; 

创建过程the_first_one(iid integer)
返回(文本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';

暂停;
结束

创建过程the_second_one
返回(text2 varchar(50))
as
begin
从__irst_one(1)中选择__irst_one.text到:text2;
暂停;
从_first_one(-1)中选择__irst_one.text到:text2;
暂停;
从__irst_one(200)中选择__irst_one.text:text2;
暂停;

当异常ex_empty做
开始
text2 ='坏输入参数' / *这里我想添加... || :the_firs_one_exception_custom_message; * /
暂停;
end
end;

当我开始使用the_second_one程序,我可以看到结果:

  OK 
输入参数不正确,

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

  OK 
输入参数不良,ID必须为正值

我知道解决方案,在_first_one过程中不引发异常,而是返回错误消息。但是如果我已经在很多地方使用过,我不想改变这个机制。



在现实世界中,__irst_one插入数据库一些文档,相当有限现在我想从外部来源导入许多文档,并以非例外方式通知用户,我们有一些数据问题。

解决方案

目前不可能。异常消息在PSQL上下文中不可用。



添加此功能有一个改进请求: CORE-2382:添加将定制异常的类型和消息转换为变量的可能性。 Firebird 4已经实施,预计将于2017年发布。



您有两种选择:


  1. 如你所说,将异常消息作为返回参数传递,

  2. 处理调用存储过程的客户端中的错误。 >


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

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

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.

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.

解决方案

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

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 2017.

You have two alternatives:

  1. As you already remark, to pass the exception message as a return parameter,
  2. Handle the error in the client that calls the stored procedure.

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

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