【问题标题】:plsql propagating/raising exception from a subprogram/unitplsql 从子程序/单元传播/引发异常
【发布时间】:2014-11-04 03:49:29
【问题描述】:

假设我正在从另一个过程调用一个 plsql 过程(都在同一个包中定义)

当出现问题时,我试图从 proc1 引发一个应用程序错误以显示在 c# 程序上(proc1 将是入口调用)。当 proc1 中发生异常时,它很简单。但是如何传播在 proc2 中引发的相同错误?

我是否也必须在 proc1 中声明相同的 user_exception EXCEPTION?或者 我应该在包级别有一个全局异常变量吗?标准做法是什么? (请忽略任何代码错误,例如 exception_init 等。)我只包含了这个概念的代码..

     create or replace procedure proc1 is 
     begin

          --some plsqlcode
          proc2();

     exception
       raise_application_error( ???? );
     end proc1;

    create or replace procedure proc2 is 
    user_exception EXCEPTION;
    begin
    --do something
    if (somefalse condition) then 
       raise user_exception
    exception
      when user_exception then 
        --do some error handling
        raise;
    end proc2;

我希望我清楚地提出了这个问题。提前感谢您的建议/提示。

【问题讨论】:

    标签: plsql oracle11g


    【解决方案1】:

    如果您有包,标准做法是在包中声明异常。

    create or replace package pkg is
      user_exception exception;
    
      procedure proc1;
      procedure proc2;
    end pkg;
    
    create or replace package body pkg is
      procedure proc1 is
      begin
        proc2;
      exception
        when user_exception then
          raise;
      end;
    
      procedure proc2 is
      begin
        raise user_exception;
      end;
    end pkg;
    

    【讨论】:

      【解决方案2】:

      一种解决方案是声明异常,使其在两个过程中都可见:

      user_exception EXCEPTION;
      
      create or replace procedure proc1 is 
      begin
      
           --some plsqlcode
           proc2();
      
      exception
        when user_exception then             -- added
          raise_application_error( ???? );   -- added
        when others then
          raise_application_error( ???? );
      end proc1;
      
      create or replace procedure proc2 is 
      begin
      --do something
      if (somefalse condition) then 
         raise user_exception
      exception
        when user_exception then 
          --do some error handling
          raise;
      end proc2;
      

      分享和享受。

      【讨论】:

      • 鲍勃,这是标准做法吗?我绝对可以在包规范中声明异常。
      • 如果异常不需要在包体外部可见(即仅由包体中的过程使用),我只需将其放在包体中。是的,我会说这是标准做法——我一直在编写的包中使用类似的声明。分享和享受。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      • 2014-11-28
      • 1970-01-01
      • 2016-01-04
      • 2018-08-13
      • 1970-01-01
      相关资源
      最近更新 更多