Oracle 捕获PL/SQL中的异常

Oracle 捕获PL/SQL中的异常,oracle,Oracle,我编写了一个包来在国家表中添加记录,该国家表的引用键指向使用region_id的“regions”表。因此,如果我尝试在我的countries表中添加“region_id”,并且如果regions表中不存在该值,我应该抛出异常并捕获。 我的包裹代码是: CREATE PACKAGE BODY cus7 AS v_error_code NUMBER; region_exists pls_integer; procedure addi6 (c_cntry_id in out countries.

我编写了一个包来在国家表中添加记录,该国家表的引用键指向使用region_id的“regions”表。因此,如果我尝试在我的countries表中添加“region_id”,并且如果regions表中不存在该值,我应该抛出异常并捕获。 我的包裹代码是:

CREATE PACKAGE BODY cus7 AS
v_error_code NUMBER;
region_exists pls_integer;

 procedure addi6 (c_cntry_id in out countries.country_id%type,
                                       c_cntr_name in countries.country_name%type, 
                                       c_rgn_id in countries.region_id%type)
is

begin
    begin
        select 1 into region_exists
        from regions r 
        where r.region_id = c_rgn_id;
    exception
        when no_data_found then

region_exists := 0;            
DBMS_OUTPUT.PUT_LINE('Region not present');




    end;




    if region_exists = 1 then
         insert into countries(country_id, country_name,region_id)
         values (c_cntry_id, c_cntr_name, c_rgn_id);
         DBMS_OUTPUT.PUT_LINE('Inserted');
END IF;

EXCEPTION
  WHEN dup_val_on_index
  THEN 
    c_cntry_id := null;
    DBMS_OUTPUT.PUT_LINE('Already present');

end addi6;

END cus7;
/
现在,在我的过程中,一切正常,除了我执行如下添加操作:

DECLARE
   outputValue CHAR(2) := 'KO';
begin
  cus7.addi6(outputValue,'KOREA',5);

end;
/
除了得到我自己的消息“未找到区域”,我还得到ORA-01403-未找到数据

我的问题是,是否有方法捕获此异常或避免显示


提前发送

是的,您所要做的就是在其他异常块中添加,以便捕获所有其他可能的或异常

EXCEPTION
    WHEN dup_val_on_index
        THEN 
            c_cntry_id := null;
            DBMS_OUTPUT.PUT_LINE('Already present')
  WHEN OTHERS
        THEN 
            DBMS_OUTPUT.PUT_LINE('Another Error');
            -- other logic here
下面是一个关于PL/SQL中错误处理的示例