Oracle PL/SQL - 使用自定义 SQLERRM 引发用户定义的异常
是否可以创建用户定义的异常并能够更改 SQLERRM?
例如:
DECLARE
ex_custom EXCEPTION;
BEGIN
RAISE ex_custom;
EXCEPTION
WHEN ex_custom THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
/
输出为“用户定义的异常”。是否可以更改该消息?
编辑:这里有一些更多细节。
我希望这能说明我正在努力做得更好。
DECLARE
l_table_status VARCHAR2(8);
l_index_status VARCHAR2(8);
l_table_name VARCHAR2(30) := 'TEST';
l_index_name VARCHAR2(30) := 'IDX_TEST';
ex_no_metadata EXCEPTION;
BEGIN
BEGIN
SELECT STATUS
INTO l_table_status
FROM USER_TABLES
WHERE TABLE_NAME = l_table_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
-- raise exception here with message saying
-- "Table metadata does not exist."
RAISE ex_no_metadata;
END;
BEGIN
SELECT STATUS
INTO l_index_status
FROM USER_INDEXES
WHERE INDEX_NAME = l_index_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
-- raise exception here with message saying
-- "Index metadata does not exist."
RAISE ex_no_metadata;
END;
EXCEPTION
WHEN ex_no_metadata THEN
DBMS_OUTPUT.PUT_LINE('Exception will be handled by handle_no_metadata_exception(SQLERRM) procedure here.');
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
/
事实上,这样的子块有几十个。我想知道是否有一种方法可以为每个子块引发一个用户定义的异常,但让它给出不同的消息,而不是为每个子块创建单独的用户定义的异常。
在 .NET 中,这有点像有一个如下所示的自定义异常:
public class ColorException : Exception
{
public ColorException(string message)
: base(message)
{
}
}
然后,一个方法将具有如下所示的内容:
if (isRed)
{
throw new ColorException("Red is not allowed!");
}
if (isBlack)
{
throw new ColorException("Black is not allowed!");
}
if (isBlue)
{
throw new ColorException("Blue is not allowed!");
}
Is it possible to create user-defined exceptions and be able to change the SQLERRM?
For example:
DECLARE
ex_custom EXCEPTION;
BEGIN
RAISE ex_custom;
EXCEPTION
WHEN ex_custom THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
/
The output is "User-Defined Exception". Is it possible to change that message?
EDIT: Here is some more detail.
I hope this one illustrates what I'm trying to do better.
DECLARE
l_table_status VARCHAR2(8);
l_index_status VARCHAR2(8);
l_table_name VARCHAR2(30) := 'TEST';
l_index_name VARCHAR2(30) := 'IDX_TEST';
ex_no_metadata EXCEPTION;
BEGIN
BEGIN
SELECT STATUS
INTO l_table_status
FROM USER_TABLES
WHERE TABLE_NAME = l_table_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
-- raise exception here with message saying
-- "Table metadata does not exist."
RAISE ex_no_metadata;
END;
BEGIN
SELECT STATUS
INTO l_index_status
FROM USER_INDEXES
WHERE INDEX_NAME = l_index_name;
EXCEPTION
WHEN NO_DATA_FOUND THEN
-- raise exception here with message saying
-- "Index metadata does not exist."
RAISE ex_no_metadata;
END;
EXCEPTION
WHEN ex_no_metadata THEN
DBMS_OUTPUT.PUT_LINE('Exception will be handled by handle_no_metadata_exception(SQLERRM) procedure here.');
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
/
In reality, there are dozens of those sub-blocks. I'm wondering if there's a way to have a single user-defined exception for each of those sub-blocks to raise, but have it give a different message, instead of creating a separate user-defined exception for each sub-block.
In .NET, it would be sort of like having a custom exception like this:
public class ColorException : Exception
{
public ColorException(string message)
: base(message)
{
}
}
And then, a method would have something like this:
if (isRed)
{
throw new ColorException("Red is not allowed!");
}
if (isBlack)
{
throw new ColorException("Black is not allowed!");
}
if (isBlue)
{
throw new ColorException("Blue is not allowed!");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的。您只需使用
RAISE_APPLICATION_ERROR
函数即可。如果您还想命名异常,则需要使用EXCEPTION_INIT
pragma 将错误号与指定的异常相关联。像这样的东西Yes. You just have to use the
RAISE_APPLICATION_ERROR
function. If you also want to name your exception, you'll need to use theEXCEPTION_INIT
pragma in order to associate the error number to the named exception. Something like您可以像这样使用 RAISE_APPLICATION_ERROR:
这将引发如下所示的异常:
错误号可以是 -20001 到 -20999 之间的任何值。
You could use RAISE_APPLICATION_ERROR like this:
That will raise an exception that looks like:
The error number can be anything between -20001 and -20999.
我通常会丢失所有
-20001
类型的错误代码,因此我尝试将所有应用程序错误合并到一个漂亮的包中,如下所示:然后调用
errors.raise_err(errors.invalid_foo_num , '可选的额外文本')
使用它,如下所示:产生以下输出:
I usually lose track of all of my
-20001
-type error codes, so I try to consolidate all my application errors into a nice package like such:Then call
errors.raise_err(errors.invalid_foo_num, 'optional extra text')
to use it, like such:produces this output:
ORACLE 11g 输出将是这样的:
ORACLE 11g output will be like this: