plsql/cursors处理异常并返回执行流程
我正在尝试执行游标并希望它完成循环,即使存在一些异常。
我想做的是“捕获”所有异常,并可能记录一些内容或不执行任何操作,然后返回到流程。代码如下所示:
FOR line IN my_cursor
LOOP
begin
if<condition> then
GOTO pass;
else
<<do_something>>
exception
when others then
sys.dbms_output.put_line('say something');
end if;
<<pass>> null;
end
END LOOP;
脚本无法编译。
例外情况可能存在一些语法错误,但我也不太了解语义。就像我不确定处理异常后是否可以返回执行流程。
ps:DB是10g,里面没有CONTINUE。因此使用GOTO。
I am trying to execute a cursor and want it to complete the loop even if there is some exception.
What I trying to do is "catch" all the exception and probably log something or do nothing and then return back to the flow . Here is how the code looks like:
FOR line IN my_cursor
LOOP
begin
if<condition> then
GOTO pass;
else
<<do_something>>
exception
when others then
sys.dbms_output.put_line('say something');
end if;
<<pass>> null;
end
END LOOP;
The script doesn't compile.
There is probably some syntax error with the exception, but I am also not aware of semantics very well. Like I am not sure if you can return back to execution flow after handling an exception.
p.s: The DB is 10g and there is not CONTINUE in it. Hence using GOTO.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将要在循环中执行的代码放入其自己的块中,然后您可以使用该块异常部分来处理循环迭代期间的任何问题。
一旦处理了该迭代的异常,下一个循环迭代将开始,
例如:
为了进一步说明这一点,在下面的示例中,我声明了一个异常类型的局部变量。我循环遍历数字 1 到 10,在第二次循环迭代期间,if 语句为 true,并且处理传递到异常处理程序。处理异常后,循环的下一次迭代开始。
Put the code that you want to execute within the loop in it's own block and then you can use that blocks exception section to handle any problems during the loop iteration.
Once the exception for that iteration is handled, the next loop iteration will start
e.g.:
To illustrate this further, in the example below, I have declared a local variable of type exception. I am looping through numbers 1 to 10, during the second loop iteration, the if statement is true and processing passes to the exception handler. Once the exception is handled, the next iteration of the loop begins.