2014-11-03 71 views
0

提高例外,比方说,我是从另一个程序(在同一个包中的所有定义)PLSQL传播/从子程序/单元

我试图从PROC1提高应用程序错误调用PLSQL过程将被显示在ac#程序(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; 

我希望我是在制定这个问题清楚了。在此先感谢您的建议/提示。

回答

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; 
1

一个解决方案是因此它的可见的声明异常在两个程序:

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; 

分享和享受。

+0

鲍勃,这是标准做法吗?我可以在包规范中声明异常。 – cableload 2014-11-03 18:31:41

+0

如果异常不需要在包体外部可见(即仅由程序包体内的过程使用),我只是将它放在包体中。是的,我会说这是标准做法 - 我在我一直编写的软件包中使用类似的声明。分享并享受。 – 2014-11-03 21:50:54

+0

感谢鲍勃。有用 ! – cableload 2014-11-05 15:10:53