[英]Exception handling in pl/sql
我有一個存儲過程
create or replace procedure Trial
is
Begin
---Block A--
EXCEPTION
when others then
insert into error_log values('error');
--Block A ends----
--Block B ----
----Block B ends---
end;
我希望塊 B 中的代碼在所有條件下執行,即是否引發了塊 A 中的異常。使用上面的代碼,塊 B 僅在引發異常時執行。 這個怎么做。 請幫忙。
您可以創建嵌套塊:
create or replace procedure Trial
is
Begin
begin
---Block A--
EXCEPTION
when others then
insert into error_log values('error');
end;
begin
--Block B ----
end;
end;
請注意,捕獲所有異常而不引發它們是一種常見的反模式。 您可能還想考慮一個自治事務,以便在回滾后保留錯誤日志。
所以你可能會更好地使用這樣的東西:
create or replace procedure Trial
is
procedure finally is
begin
--Block B ----
end;
procedure logerr (msg varchar2) is
PRAGMA AUTONOMOUS_TRANSACTION;
begin
insert into error_log values(msg);
commit;
end;
Begin
begin
---Block A--
EXCEPTION
when others then
logerr(SQLERRM);
finally;
raise;
end;
finally;
end;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.