2013-10-21 3 views
1

저는 다른 사람들의 코드를 담당 한 연수 개발자입니다. 따라서 대부분의 작업이 자신의 작업을 수정하게 될 것입니다. Oracle 10g에서 Report Builder를 사용하고 있습니다.예외로 인해 코드가 실행되지 않습니다.

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; 

이 마지막으로 다른 블록을 제외한 모든 기능은 다음과 같습니다

나는 공식에서 다음과 같은 설정이있다. no_data_found 시도했지만 여전히 작동하지 않습니다.

@tbone. 어떻게해야할지 모르겠다. 지금까지 운이 좋지 않은 RAISE에서 인터넷 검색을 한 적이 있습니다.

+2

실제로 예외를 발생시키고 null을 반환하는 대신 예외를 발생 시키길 원할 수 있습니다. 이것이 당신이 무슨 일이 일어 났는지 알지 못하는 이유입니다. – tbone

+0

더 많은 코드를 표시하고 실행되지 않은 코드를 지적 할 수 있습니까? 'EXCEPTION' 이후의 코드가 실행되지 않으면 예외가 항상 throw되고 블록이'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; 

는 각 EXCEPTION에 대한 BEGIN/END 필요합니다

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; 

는 또한 RAISE 다음에하지 WHEN OTHERS의 사용이 나쁜 code smell입니다 있습니다. 모든 오류를 무시하고 싶지 않으므로 구체적으로 작성하십시오. 보통은 NO_DATA_FOUND을 잡기 만하면됩니다.

+0

감사합니다. Vincent. RAISE 사용법을 보여 주실 수 있습니까? NO_DATA_FOUND에 대해 언급 한 것처럼 작동하지 않습니다. 나는 Toad에서 수정 된 버전을 작성하고 거기서 다른 것들을 시도했다. 행운은 아직 없습니다. 나는 RAISE 예제를 위해 다른 procs에서 스크래치 할 것이다. – Arend

+0

@arend 예제에서'RETURN NULL '다음에 코드를 두는 것은 이치에 맞지 않습니다.이 코드는 결코 실행되지 않습니다. 블록은'BEGIN/END'에 의해 정의되므로'END'는 아마'ELSE '대신에'RETURN NULL'뒤에 있어야합니다. –

+0

['raise_application_error'] (http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/errors.htm#i1871)를 사용하여 예외를 발생시킬 수 있습니다. 하지만 대부분의 경우 오류를 로깅하지 않으면 '다른 사람'을 사용하지 않는 것이 좋습니다. 알 수없는 오류가 전파되면 예기치 않은 예외를 포착 할 필요가 없습니다. –

관련 문제