过程中的 PL/SQL 异常处理

发布于 2024-12-04 06:43:57 字数 839 浏览 0 评论 0原文

这是一个关于最佳实践的问题。我有一个与此类似的 PL/SQL 块。

DECLARE

    --work variables

    PROCEDURE p1(in_parameter1, out_parameter1, out_parameter2...) IS
    BEGIN    
        --do stuff
        --select ( ... ) into ( ... ) from t1 where ( ... )
    END;

    PROCEDURE p2(in_parameter1, out_parameter1, out_parameter2...) IS
    BEGIN 
        --do stuff
        --insert/update tables
        --do more stuff
    END;    

BEGIN -- MAIN PROCESS STARTS HERE

    open c1;
    fetch c1 into c1RowData;
    EXIT WHEN c1%NOTFOUND    
     --call procedure1
     --do stuff
     --call procedure2
     --do stuff
     --do stuff
     --call procedure1
     --call procedure2
END;
/
EXIT;

过程 p1 和 p2 中的语句可能会引发异常(NO_DATA_FOUND、DUP_VAL_ON_INDEX,...)。

您认为处理这种异常的最佳方法是什么?它们应该在程序内部处理还是您认为我应该用 TRY-CATCH 块包围主体中程序的每个调用?

This is kind of a question about best practices. I have a PL/SQL block similar to this

DECLARE

    --work variables

    PROCEDURE p1(in_parameter1, out_parameter1, out_parameter2...) IS
    BEGIN    
        --do stuff
        --select ( ... ) into ( ... ) from t1 where ( ... )
    END;

    PROCEDURE p2(in_parameter1, out_parameter1, out_parameter2...) IS
    BEGIN 
        --do stuff
        --insert/update tables
        --do more stuff
    END;    

BEGIN -- MAIN PROCESS STARTS HERE

    open c1;
    fetch c1 into c1RowData;
    EXIT WHEN c1%NOTFOUND    
     --call procedure1
     --do stuff
     --call procedure2
     --do stuff
     --do stuff
     --call procedure1
     --call procedure2
END;
/
EXIT;

The statements in procedures p1 and p2 could possibly raise exceptions ( NO_DATA_FOUND, DUP_VAL_ON_INDEX, ... ).

What do you think is the best way to handle this exceptions? Should they be handled inside the procedures or do you think that I should surround every call the procedures in the main body with a TRY-CATCH block?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月打碎记忆 2024-12-11 06:43:57

就我个人而言,我会在他们被抛出的过程中抓住他们。这意味着您对外部处理方式有更多的控制权。例如,您可以将它们作为用户定义的异常再次抛出,您可以用有关到底出了什么问题的更多信息来修饰它们。

'Failed to find a matching row in table a for value b'

在过程之外比过程更具描述性

'no data found'

但这实际上取决于:

  1. 调用应用程序的错误报告要求
  2. 在过程中“执行操作”时实现的实际功能

例如,假设您想要运行过程 2,即使选择在过程 1 中未找到任何行。您需要捕获过程 1 中的异常并忽略它。如果没有,那么它会被抛出到过程 2 中的异常处理程序。

或者,假设您希望过程 1 在 select 没有找到任何内容的情况下插入一行,在这种情况下您需要捕获异常并在异常处理程序中执行插入。

现在,在有人攻击我之前,我不建议您使用异常处理程序来控制代码中的执行流程,这些示例是理论上的,但希望您能理解。

Personally I'd catch them inside the procedures they're thrown from. This means that you have much more control with regards to how they're handled externally. For example, you can throw them again as user defined exceptions which you can embellish with more information about exactly what went wrong.

'Failed to find a matching row in table a for value b'

is much more descriptive outside the procedure than

'no data found'

But this is realy dependent on:

  1. The error reporting requirements of the calling application
  2. The actual functionality implemented where you 'do stuff' within the procedures

For example, say you wanted to run procedure 2 even though the select in procedure 1 found no rows.. You'd need to catch the exception in procedure 1 and ignore it. If you didn't, then it'd be thrown to the exception handler in procedure 2.

Or, say you wanted procedure 1 to insert a row in the case where the select didn't find anything, in this case you'd need to catch the exception and perform the insert in the exception handler.

Now, before anyone jumps on me, I'm not recommending you use exception handlers for contrlling the flow of execution within your code, these examples are the theoretical, but hopefully you get the idea..

故笙诉离歌 2024-12-11 06:43:57

您应该尝试在源头处理异常(即在引发异常的过程中)。这为您提供了更大的范围来报告问题发生的位置,并且通常更有机会从容地纠正问题,而无需将令人讨厌的 Oracle 错误消息向上传递给用户。

当然,如果确实需要,您可以处理错误并重新引发错误,但正如 StevieG 回答的那样,您还可以引发用户定义的异常,这些异常通常更特定于应用程序,并且对您的用户/其他 PL/SQL 代码更有帮助。

这里有一个关于自定义错误处理的 ASKTOM 讨论:
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:4684561825338

You should try to handle exceptions at the source (i.e. within the procedure that raises them). This gives you greater scope for reporting where the issue occurred and, usually, a greater chance of being able to gracefully rectify the issue without passing a nasty Oracle error message up the stack to the user.

Of course you can both handle the error and re-raise it if you really needed to but as StevieG answered you can also raise user defined exceptions which are generally more application specific and more helpful to your users/other PL/SQL code.

There is an ASKTOM discussion on custom error handling here:
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:4684561825338

老娘不死你永远是小三 2024-12-11 06:43:57

最好在过程内部处理异常。很快就会有结果。不仅如此,如果我们在其他一些函数中重用相同的过程,我们也不需要处理异常。我个人的观点,程序本身内部能够处理异常是最好的。

It is best when the exceptions handled inside the procedure. It will be resulting quick. Not only that if we reuse the same procedure in some other functions, there also we don't need to handle exceptions. I my point of view, exceptions can handle inside the procedure itself is the best one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文