Answers:
您有两种选择:
通过数字直接引用异常:
BEGIN
EXECUTE IMMEDIATE 'CREATE SEQUENCE S_TEST START WITH 1 INCREMENT BY 1';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE = -955 THEN
NULL; -- suppresses ORA-00955 exception
ELSE
RAISE;
END IF;
END;
另一种选择是使用EXCEPTION_INIT
Pragma指令将已知的Oracle错误号绑定到用户定义的异常。
DECLARE
name_in_use exception; --declare a user defined exception
pragma exception_init( name_in_use, -955 ); --bind the error code to the above
BEGIN
EXECUTE IMMEDIATE 'CREATE SEQUENCE S_TEST START WITH 1 INCREMENT BY 1';
EXCEPTION
when name_in_use then
null; --suppress ORA-00955 exception
END;
BTW是否有仅通过提供错误代码来捕获错误的语法?
是的,我已经在第一个示例中进行了演示
有关此变化的更多阅读:
sqlcode
不是955 =,请在您的其他人中加薪)
与Sathya已经提出的建议类似,但我想尽可能避免when others
使用-未处理的异常通常是未明确处理的异常的正确结果:
create sequence foo;
/*
sequence FOO created.
*/
declare
name_is_already_used_955 exception;
pragma exception_init(name_is_already_used_955,-955);
begin
execute immediate 'create sequence foo';
exception when name_is_already_used_955 then null;
end;
/
/*
anonymous block completed
*/