在 PL/SQL 中使用全局异常是一种不好的做法吗?
执行下面的代码的做法是一种不好的做法吗?我会因为写它而遇到不好的事情吗?
编辑:这只是一个例子。我不会使用 dbms_output 进行任何实际的错误报告。
CREATE OR REPLACE PACKAGE my_package
AS
PROCEDURE master;
END;
/
CREATE OR REPLACE PACKAGE BODY my_package
AS
my_global_interrupt EXCEPTION;
PROCEDURE my_private_procedure
IS
BEGIN
-- in case some flag is raised, raise exception to stop process and prepare for resume
RAISE my_global_interrupt;
END;
PROCEDURE master
IS
BEGIN
my_private_procedure;
EXCEPTION
WHEN my_global_interrupt THEN
dbms_output.put_line('global interrupt, ');
-- prepare to resume
END;
END;
/
Is it a bad practice to do what the code below does? Will bad things happen to me for writing it?
Edit: this is just an example. I would not use dbms_output for any real error reporting.
CREATE OR REPLACE PACKAGE my_package
AS
PROCEDURE master;
END;
/
CREATE OR REPLACE PACKAGE BODY my_package
AS
my_global_interrupt EXCEPTION;
PROCEDURE my_private_procedure
IS
BEGIN
-- in case some flag is raised, raise exception to stop process and prepare for resume
RAISE my_global_interrupt;
END;
PROCEDURE master
IS
BEGIN
my_private_procedure;
EXCEPTION
WHEN my_global_interrupt THEN
dbms_output.put_line('global interrupt, ');
-- prepare to resume
END;
END;
/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
相反,全局定义的用户例外是一种很好的做法。考虑以下包体骨架。
使用全局声明的异常使
master
过程中的异常处理更加容易。另外,请记住,有时我们希望将异常传播到包之外,例如调用我们公开声明的过程的程序。我们可以通过在包规范中定义异常来做到这一点。这意味着其他过程可以引用它们...
我们还可以将此类异常与特定的错误号相关联,以便即使调用过程没有显式处理它们,它们也可以被识别。
这比一般的
ORA-06510
错误(稍微)更有帮助。On the contrary globally defined user exceptions is good practice. Consider the following skeleton of a package body.
The use of globally declared exceptions makes exception handling in the
master
procedure easier.Also, bear in mind that sometimes we want to propagate the exception beyond the package, to say a program which calls our publicly declared procedure. We can do that by defining our exceptions in the package spec. This means other proecdures can reference them...
We can also associate such exceptions with specific error numbers, so that they are recognisable even if the calling procedure doesn't explicitly handled them.
That's (slightly) more helpful than the generic
ORA-06510
error.在我看来,这很合理,前提是您很高兴在中断条件之后可以恢复处理。如果您打算以某种方式记录中断,最好使用自治事务将一行插入到日志表中。在整个过程完成之前,您不会从 DBMS_OUTPUT 中看到任何内容。然后您将立即看到所有 DBMS_OUTPUT。
Looks reasonable enough to me, provided you're happy that after the interrupt condition it's OK to resume processing. If you are going to log the interrupt in some way, it's probably better to insert a row into a log table using an autonomous transaction. You won't see anything from DBMS_OUTPUT until the whole procedure finishes. Then you'll see all the DBMS_OUTPUT at once.