2013-10-21 64 views
1

我是一名实习开发人员,他负责管理其他人的代码,因此我的大部分工作都将修改其工作。我在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。我不知道该怎么做。到目前为止,我用一些Google搜索了一些Google搜索。

+2

您可能希望实际引发异常而不是陷入它并返回null(这就是为什么您不知道发生了什么)。 – tbone

+0

你能告诉我们更多的代码并指出未执行的代码吗?如果EXCEPTION之后的代码从未执行过,则意味着总是抛出异常,并以“RETURN NULL”结尾。 –

+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

+0

谢谢文森特。你能告诉我如何使用RAISE?如上所述NO_DATA_FOUND也不起作用。我在蟾蜍里写了一个修改过的版本,并尝试了不同的东西。没有运气。对于RAISE的例子,我会在其他过程中抓狂。 – Arend

+0

@arend在你的例子中,在'RETURN NULL'后面加上代码是没有意义的:这段代码永远不会运行。块由'BEGIN/END'定义,所以你的'END'应该在'RETURN NULL'之后,而不是'ELSE'之前。 –

+0

您可以使用['raise_application_error'](http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/errors.htm#i1871)引发异常。但是在大多数情况下,如果你没有记录错误,你最好不要使用'WHE OTHERS'。让未知的错误传播,捕捉意外的异常没有意义。 –

相关问题