2013-12-23 69 views
0

錯誤在確定的行中時是否有可能發生異常?是否可以在PL/SQL中對精確行進行異常處理?

例如,這是我的代碼片段:

l_XMLvalue := l_xmltype.extract('/SHOW_SHIPMENT_005/DATAAREA/SHOW_SHIPMENT/SHIPMENT/SHIPITEM/QUANTITY/VALUE/text()').getStringVal(); 
dbms_output.put_line('QUANTITY: '||l_XMLvalue); 
l_XMLdocument_id := l_xmltype.extract('/SHOW_SHIPMENT_005/DATAAREA/SHOW_SHIPMENT/SHIPMENT/SHIPITEM/DOCUMNTREF/DOCUMENTID/text()').getStringVal(); 
dbms_output.put_line('DOCUMENT ID:' ||l_XMLdocument_id); 
l_XMLdoc_release := l_xmltype.extract('/SHOW_SHIPMENT_005/DATAAREA/SHOW_SHIPMENT/SHIPMENT/SHIPITEM/DOCUMNTREF/USERAREA/DOCUMENTRELEASE/text()').getStringVal(); 
dbms_output.put_line('DOCUMENT RELEASE:' ||l_XMLdoc_release); 
l_XMLlinenum := l_xmltype.extract('/SHOW_SHIPMENT_005/DATAAREA/SHOW_SHIPMENT/SHIPMENT/SHIPITEM/DOCUMNTREF/LINENUM/text()').getStringVal(); 
dbms_output.put_line('LINE NUMBER:' ||l_XMLlinenum); 
l_XMLsch_lnum := l_xmltype.extract('/SHOW_SHIPMENT_005/DATAAREA/SHOW_SHIPMENT/SHIPMENT/SHIPITEM/DOCUMNTREF/SCHLINENUM/text()').getStringVal(); 
dbms_output.put_line('SCHIPMENT LINE NUMBER:' ||l_XMLsch_lnum); 

當函數提取正在試圖獲取價值,會有NULL,該程序將停止。這是可能的例外:

WHEN SELF_IS_NULL THEN 
dbms_output.put_line('There is a problem with empty values in XML file'); 

這是我想做的事情是檢查在哪一行發生錯誤,並寫異常其準確值是錯誤

回答

0

嘗試創建函數extract函數的包裝函數,並確定一個EXCEPTION SELF_IS_NULL並使用RAISE SELF_IS_NULL;如果它返回NULL。

dbms_output.put_line(SQLERRM||chr(10)||dbms_utility.format_error_backtrace);

請把這個在您的異常塊。它打印從它起源(線NO)

0

一種方式來完成這個堆棧跟蹤是使用檢查點變量,類似以下內容:

BEGIN 
    strXMLelement := '/SHOW_SHIPMENT_005/DATAAREA/SHOW_SHIPMENT/SHIPMENT/SHIPITEM/QUANTITY/VALUE/text()'; 
    l_XMLvalue := l_xmltype.extract(strXMLelement).getStringVal(); 
    dbms_output.put_line('QUANTITY: '||l_XMLvalue); 

    strXMLelement := '/SHOW_SHIPMENT_005/DATAAREA/SHOW_SHIPMENT/SHIPMENT/SHIPITEM/DOCUMNTREF/DOCUMENTID/text()'; 
    l_XMLdocument_id := l_xmltype.extract(strXMLelement).getStringVal(); 
    dbms_output.put_line('DOCUMENT ID:' ||l_XMLdocument_id); 

    strXMLelement := '/SHOW_SHIPMENT_005/DATAAREA/SHOW_SHIPMENT/SHIPMENT/SHIPITEM/DOCUMNTREF/USERAREA/DOCUMENTRELEASE/text()'; 
    l_XMLdoc_release := l_xmltype.extract(strXMLelement).getStringVal(); 
    dbms_output.put_line('DOCUMENT RELEASE:' ||l_XMLdoc_release); 

    strXMLelement := '/SHOW_SHIPMENT_005/DATAAREA/SHOW_SHIPMENT/SHIPMENT/SHIPITEM/DOCUMNTREF/LINENUM/text()'; 
    l_XMLlinenum := l_xmltype.extract(strXMLelement).getStringVal(); 
    dbms_output.put_line('LINE NUMBER:' ||l_XMLlinenum); 

    strXMLelement := '/SHOW_SHIPMENT_005/DATAAREA/SHOW_SHIPMENT/SHIPMENT/SHIPITEM/DOCUMNTREF/SCHLINENUM/text()'; 
    l_XMLsch_lnum := l_xmltype.extract(strXMLelement).getStringVal(); 
    dbms_output.put_line('SCHIPMENT LINE NUMBER:' ||l_XMLsch_lnum); 
EXCEPTION 
    WHEN SELF_IS_NULL THEN 
    DBMS_OUTPUT.PUT_LINE('Error extracting ' || strXMLelement || ' : ' || 
         'SQLCODE=' || SQLCODE || ' SQLERRM=' || SQLERRM); 
END; 

分享和享受。

相關問題