2013-10-21 65 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'。讓未知的錯誤傳播,捕捉意外的異常沒有意義。 –

相關問題