自定义消息的用户定义的执行 [英] User-defined execption with custom message
问题描述
想要为用户定义的异常定义自定义消息。我现在有:
声明
e异常;
pragma exception_init(e,-20100);
begin
raise e;
结束
ORA-20100:
ORA-06512:第5行
我想要的:
exec dbms_output.put_line(userenv('COMMITSCN'))
ORA-01725:USERENV('COMMITSCN')不允许在这里
ORA-06512:在SYS.STANDARD,第202行
ORA-06512:第1行
$ c $在SYS.STANDARD第202行,我们可以看到:
$ b raise USERENV_COMMITSCN_ERROR;
异常在规范中定义为:
- 添加为USERENV增强,错误1622213.
USERENV_COMMITSCN_ERROR异常;
pragma EXCEPTION_INIT(USERENV_COMMITSCN_ERROR,'-1725');
问题是:
-
如何定义消息USERENV('COMMITSCN')?
-
如何在我的代码
非常感谢您的答案!
解决方案使用RAISE_APPLICATION_ERROR过程来引发异常,可以将消息与错误相关联:
DECLARE $ b $ is EXCEPTION;
PRAGMA EXCEPTION_INIT(e,-20100);
BEGIN
RAISE_APPLICATION_ERROR(-20100,这是用户提供的消息);
EXCEPTION
WHEN e THEN
DBMS_OUTPUT.PUT_LINE('caught e:'|| SQLCODE ||''|| SQLERRM);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('catch something else:'|| SQLCODE ||''|| SQLERRM);
END;
此处的文档 - 特别是阅读标题为定义您自己的错误消息:过程RAISE_APPLICATION_ERROR的部分。
分享享受。
Want to define a custom message for a user defined exception. What I have now:
declare
e exception;
pragma exception_init (e, -20100);
begin
raise e;
end;
ORA-20100:
ORA-06512: at line 5
What I want:
exec dbms_output.put_line(userenv('COMMITSCN'))
ORA-01725: USERENV('COMMITSCN') not allowed here
ORA-06512: at "SYS.STANDARD", line 202
ORA-06512: at line 1
at "SYS.STANDARD", line 202 we can see:
raise USERENV_COMMITSCN_ERROR;
The exception is defined in specification as:
-- Added for USERENV enhancement, bug 1622213.
USERENV_COMMITSCN_ERROR exception;
pragma EXCEPTION_INIT(USERENV_COMMITSCN_ERROR, '-1725');
The questions are:
How the message "USERENV('COMMITSCN') not allowed here" is defined?
How to do it in my code?
Thanks a lot for your answers!
解决方案 Using the RAISE_APPLICATION_ERROR procedure to raise the exception allows you to associate a message with the error:
DECLARE
e EXCEPTION;
PRAGMA EXCEPTION_INIT (e, -20100);
BEGIN
RAISE_APPLICATION_ERROR(-20100, 'This is the user-supplied message');
EXCEPTION
WHEN e THEN
DBMS_OUTPUT.PUT_LINE('Caught e: ' || SQLCODE || ' ' || SQLERRM);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Caught something else: ' || SQLCODE || ' ' || SQLERRM);
END;
Documentation here - in particular, read the section titled "Defining Your Own Error Messages: Procedure RAISE_APPLICATION_ERROR".
Share and enjoy.
这篇关于自定义消息的用户定义的执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!