oracle “当其他人加注时例外”会做些什么吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14685467/
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
Does `EXCEPTION WHEN OTHERS THEN RAISE` do something?
提问by álvaro González
Being still a newbie in PL/SQL, I've been copying and pasting around the following trigger:
作为 PL/SQL 的新手,我一直在复制和粘贴以下触发器:
CREATE OR REPLACE TRIGGER FOO_TRG1
BEFORE INSERT
ON FOO
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
IF :NEW.FOO_ID IS NULL THEN
SELECT FOO_SEQ1.NEXTVAL INTO :NEW.FOO_ID FROM DUAL;
END IF;
EXCEPTION
WHEN OTHERS THEN RAISE;
END FOO_TRG1;
/
ALTER TRIGGER FOO_TRG1 ENABLE;
I suspect that the included exception handling code does nothing at all and could simply get removed, since I'll get an error message anyway if something goes wrong. Am I right?
我怀疑包含的异常处理代码根本不执行任何操作,可以简单地将其删除,因为如果出现问题,无论如何我都会收到错误消息。我对吗?
(I guess such code is the result of further editing prior code.)
(我猜这样的代码是进一步编辑先前代码的结果。)
回答by DazzaL
yes, that exception does nothing but raise the same error out. also it serves to mask the real line number of the error. i'd remove that if I were you.
是的,该异常只会引发相同的错误。它还用于掩盖错误的实际行号。如果我是你,我会删除它。
eg:
例如:
SQL> declare
2 v number;
3 begin
4 select 1 into v from dual;
5 select 'a' into v from dual;
6 exception
7 when others
8 then
9 raise;
10 end;
11 /
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 9
vs:
对比:
SQL> declare
2 v number;
3 begin
4 select 1 into v from dual;
5 select 'a' into v from dual;
6 end;
7 /
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 5
the line number in the first one is pointing to the raise instead of the real line number. it can make tracking down errors harder.
第一个中的行号指向加薪而不是实际行号。它会使跟踪错误变得更加困难。
回答by Arthritic Toe
It doesn't just do nothing at all, it is verging on criminal. It basically says "Just for fun, I'm going to pretend that the error occurred at this line rather than the real line."
它不只是什么都不做,它已接近犯罪。它基本上是说“只是为了好玩,我将假装错误发生在这一行而不是真正的行上。”
Exception handling is the most misunderstood aspect of the entire language. The above is very common, and I think it stems from the basic misconception that an 'unhandled exception' is a bad thing (it almost begs the question - why didn't you handle it?). If only Oracle had used the terminology 'exception occured' or 'exception encountered', us poor folks who have to support this code would have far fewer 'WHEN OTHERS' exception handlers making our lives miserable.
异常处理是整个语言中最容易被误解的方面。以上是很常见的,我认为它源于基本的误解,即“未处理的异常”是一件坏事(它几乎回避了一个问题——你为什么不处理它?)。如果 Oracle 只使用术语“发生异常”或“遇到异常”,我们这些必须支持此代码的穷人就会少得多的“WHEN OTHERS”异常处理程序让我们的生活变得悲惨。
回答by EAmez
It does nothing at all but mask error line, as @DazzaL points. But it serves as a reminder, telling you that the code above can raise an exception that should be treated but treatment remains undefined.
正如@DazzaL 指出的那样,它除了屏蔽错误线之外什么都不做。但它作为一个提醒,告诉你上面的代码可以引发一个应该处理的异常,但处理仍然未定义。
In other programming languages you can create a try/catch block automatically (e.g. in Eclipse/Java, Netbeans/PHP or Visual Studio/C#), and it is created with a catch (like the "when others") wich default behaviour is throw again the exception, while programmer decide what should be done when an exception is raised.
在其他编程语言中,您可以自动创建 try/catch 块(例如在 Eclipse/Java、Netbeans/PHP 或 Visual Studio/C# 中),并且它是使用 catch 创建的(如“当其他人”),默认行为是 throw再次出现异常,而程序员决定在引发异常时应该做什么。