oracle PL/SQL 异常处理:什么都不做(忽略异常)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27566867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PL/SQL exception handling: do nothing (ignore exception)
提问by silentsurfer
This is a question I am asked very frequently. Since I couldn't find any exact duplicate on stackoverflow, I thought I'd post it as a reference.
这是我经常被问到的一个问题。由于我在 stackoverflow 上找不到任何完全相同的副本,因此我想将其发布为参考。
Question: In PL/SQL, I know how to catch exceptions and execute code when they are caught, and how to propagate them to the calling block. For example, in the following procedure, the NO_DATA_FOUND exception is handled directly, while all other exceptions are raised to the calling block:
问题:在 PL/SQL 中,我知道如何捕获异常并在捕获时执行代码,以及如何将它们传播到调用块。例如,在以下过程中,直接处理 NO_DATA_FOUND 异常,而将所有其他异常引发给调用块:
CREATE OR REPLACE PROCEDURE MY_PROCEDURE()
IS
BEGIN
do_stuff();
EXCEPTION
WHEN NO_DATA_FOUND THEN
-- Do something
handle_exception();
WHEN OTHERS THEN
-- Propagate exception
RAISE;
END;
But what command should I use to ignore one or all raised exceptions and return execution control back to the calling block?
但是我应该使用什么命令来忽略一个或所有引发的异常并将执行控制返回给调用块?
回答by silentsurfer
While I agree that 99% of the time it is bad practice to silently ignore exceptions without at least logging them somewhere, there are specific situations where this is perfectly acceptable.
虽然我同意在 99% 的情况下静默忽略异常而不至少在某处记录它们是不好的做法,但在某些特定情况下这是完全可以接受的。
In these situations, NULL is your friend:
在这些情况下,NULL 是您的朋友:
[...]
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
Two typical situations where ignoring exceptions might be desirable are:
可能需要忽略异常的两种典型情况是:
1) Your code contains a statement which you know will fail occasionally and you don't want this fact to interrupt your program flow. In this case, you should enclose you statement in a nested block, as the following example shows:
1) 您的代码包含一个您知道偶尔会失败的语句,并且您不希望此事实中断您的程序流程。在这种情况下,您应该将语句括在嵌套块中,如下例所示:
CREATE OR REPLACE PROCEDURE MY_PROCEDURE()
IS
l_empoyee_name EMPLOYEES.EMPLOYEE_NAME%TYPE;
BEGIN
-- Catch potential NO_DATA_FOUND exception and continue
BEGIN
SELECT EMPLOYEE_NAME
INTO l_empoyee_name
FROM EMPLOYEES
WHERE EMPLOYEE_ID = 12345;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
WHEN OTHERS THEN
RAISE;
END;
do_stuff();
EXCEPTION
WHEN OTHERS THEN
-- Propagate exception
RAISE;
END;
Note that PL/SQL generally does not allow for the On Error Resume Next type of exception handling known from Visual Basic, where all exceptions are ignored and the program continues to run as if nothing happened (see On error resume next type of error handling in PL/SQL oracle). You need to explicitly enclose potentially failing statements in a nested block.
请注意,PL/SQL 通常不允许 Visual Basic 中已知的 On Error Resume Next 类型的异常处理,其中所有异常都被忽略,程序继续运行,就好像什么都没发生一样(参见On error resume next type of error processing in PL/SQL 预言机)。您需要在嵌套块中显式包含可能失败的语句。
2) Your procedure is so unimportant that ignoring all exceptions it throws will not affect your main program logic. (However, this is very rarely the case and can often result in a debugging nightmare in the long run)
2)你的程序是如此不重要,忽略它抛出的所有异常不会影响你的主程序逻辑。(但是,这种情况很少发生,从长远来看,通常会导致调试噩梦)
BEGIN
do_stuff();
EXCEPTION
WHEN OTHERS THEN
-- Ignore all exceptions and return control to calling block
NULL;
END;
回答by Ananth N
Another scenario when it does make sense to silently ignore exception: When you call a script that is expected to create an object if it does not exist, and you do not have a create-or-replace syntax for that object. PLSQL objects have a create-or-replace syntax, but tables and indexes do not. Then we can put such scripts in a block and ignore the raised exception.
静默忽略异常确实有意义的另一种情况:当您调用脚本时,如果该脚本不存在,则应创建该对象,并且您没有该对象的创建或替换语法。PLSQL 对象具有创建或替换语法,但表和索引没有。然后我们可以将这些脚本放在一个块中并忽略引发的异常。