为什么我的异常会阻止代码运行

时间:2013-10-21 13:12:26

标签: oracle plsql oraclereports

我是一名实习开发人员,负责管理其他人的代码,因此我的大部分工作都将修改他们的工作。我在Oracle 10g上使用“报表生成器”。

我在公式中有以下设置:

function get_addressFormula return Char is
begin
if :payee_ctc_id is not null then
begin
 select  a.address
        ,a.address2
        ,a.address3
        ,g.location
            ,g.ppostcode
 into    :address1
        ,:address2
        ,:address3
        ,:address4
        ,:postcode

 from ctc_address a
     ,geo_locations g

 where a.addresstypeid = 1  
 and   a.costcentreid = :payee_ctc_id
 and   g.locationid = a.locationid
 and   a.addressid = (select max(i.addressid)   
                      from  ctc_address i
            where i.costcentreid = :payee_ctc_id
            and   i.addresstypeid = 1);

exception
    when others then
      return null;

    while trim(:address1) is null and (trim(:address2) is not null or trim(:address2)  is not null or trim(:address4) is not null)
    loop
      :address1 := :address2; 
      :address2 := :address3; 
      :address3 := :address4; 
      :address4 := '';
    end loop;

    while trim(:address2) is null and (trim(:address3) is not null or trim(:address4) is not null)
    loop
      :address2 := :address3; 
      :address3 := :address4; 
      :address4 := '';
    end loop;

    while trim(:address3) is null and trim(:address4) is not null
    loop
      :address3 := :address4; 
      :address4 := '';
    end loop;

end;
else
 begin
  <else code>
 end;
 end if;

return 'y';
end;

除了最后一个else块之外,这是完整的功能。我尝试过no_data_found但仍然无法正常工作。

@tbone。我不知道该怎么做。到目前为止,我在RAISE上做了一些Googling,运气不大。

1 个答案:

答案 0 :(得分:4)

请参阅Block structure

<< label >> (optional)
DECLARE    -- Declarative part (optional)
  -- Declarations of local types, variables, & subprograms

BEGIN      -- Executable part (required)
  -- Statements (which can use items declared in declarative part)

[EXCEPTION -- Exception-handling part (optional)
  -- Exception handlers for exceptions (errors) raised in executable part]
END;

每个BEGIN/END都需要EXCEPTION

if :payee_id is not null then    
   begin
      <Select statement which place results into placeholders>
   exception
      when NO_DATA_FOUND then
         return null;
   end;
   <code>    
else
   <else code>
end if;

另请注意,使用WHEN OTHERS后面没有RAISE的内容不合格code smell。您不希望忽略所有错误,因此请具体。通常你只想抓住NO_DATA_FOUND