2014-11-03 33 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