Oracle 嵌套块和异常处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20218009/
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
Oracle nested blocks and exception handling
提问by redsoxlost
DECLARE
string_of_5_chars VARCHAR2(5);
BEGIN
BEGIN
string_of_5_chars := 'Steven';
EXCEPTION
WHEN value_error THEN
RAISE no_data_found;
WHEN no_data_found THEN
dbms_output.Put_line ('Inner block');
END;
EXCEPTION
WHEN no_data_found THEN
dbms_output.Put_line ('Outer block');
END;
Answer says that the output will be 'Outer block' , Can somebody explain why the inner block would not be executed ? What is the precedence of exceptions in oracle
答案说输出将是 'Outer block' ,有人可以解释为什么不执行内部块吗?oracle中异常的优先级是什么
回答by Adarsh
DECLARE
string_of_5_chars VARCHAR2(5);
BEGIN
BEGIN
string_of_5_chars := 'Steven'; -- Varchar has a size of 5 defined above. So it will throw a value_error(due to size constraints) exception.
EXCEPTION
WHEN value_error THEN -- This exception block will handle the error thrown above.
RAISE no_data_found; -- It raises a no_data _found exception which by rule has to be handled in the outer exception block. So it goes to the outer exception block.
WHEN no_data_found THEN
dbms_output.Put_line ('Inner block');
END;
EXCEPTION
WHEN no_data_found THEN
dbms_output.Put_line ('Outer block'); -- Exception is handled here which causes it to print 'Outer Block'
END;
Read herefor more information about nested exception blocks.
阅读此处了解有关嵌套异常块的更多信息。
回答by David Aldridge
You should consider the exception block's WHEN clauses as being similar to a regular CASE statement. The first WHEN that matches the condition executes, and the following WHEN clauses in that exception handler are skipped.
您应该将异常块的 WHEN 子句视为类似于常规 CASE 语句。执行第一个匹配条件的 WHEN,并跳过该异常处理程序中的以下 WHEN 子句。
Therefore the second WHEN clause in the inner exception block is not in the code execution path at all, and the outer exception block catches the no_data_found error raised by the first WHEN clause of the nested exception.
因此内部异常块中的第二个 WHEN 子句根本不在代码执行路径中,而外部异常块捕获由嵌套异常的第一个 WHEN 子句引发的 no_data_found 错误。
Exception propagation in this scenario is explained here: http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/errors.htm#LNPLS00706
这里解释了这种情况下的异常传播:http: //docs.oracle.com/cd/B28359_01/appdev.111/b28370/errors.htm#LNPLS00706
回答by Armunin
As string_of_5_chars := 'Steven';
raises an value_error
, the corresponding Exception block is entered.
Inside the Exception block the no_data_found
Exception is raised. Due to the raising-part this exception is then handled by the exception-handling of the outer block.
随着string_of_5_chars := 'Steven';
引发value_error
,进入相应的异常块。
在 Exception 块内no_data_found
引发了Exception。由于上升部分,这个异常然后由外部块的异常处理来处理。
For more information check http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/raise_statement.htm
有关更多信息,请查看http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/raise_statement.htm