问题
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.
回答1:
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:
- As you already remark, to pass the exception message as a return parameter,
- Handle the error in the client that calls the stored procedure.
来源:https://stackoverflow.com/questions/33300842/firebird-handling-exceptions-custom-message