<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to trap errors in pl/sql script? in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/how-to-trap-errors-in-pl-sql-script/m-p/2964736#M814871</link>
    <description>Hi,&lt;BR /&gt;&lt;BR /&gt;We are running pl/sql on an hp/ux system and would like to find a way to trap any errors that occur within the script.  If an oracle error would occur, in the script execution, we would like to know of its success or failure when the script is finished.&lt;BR /&gt;&lt;BR /&gt;Can anyone offer any suggestions?&lt;BR /&gt;&lt;BR /&gt;Thanks in advance,&lt;BR /&gt;Randy</description>
    <pubDate>Fri, 02 May 2003 14:31:11 GMT</pubDate>
    <dc:creator>Randy Hagedorn</dc:creator>
    <dc:date>2003-05-02T14:31:11Z</dc:date>
    <item>
      <title>How to trap errors in pl/sql script?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/how-to-trap-errors-in-pl-sql-script/m-p/2964736#M814871</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;We are running pl/sql on an hp/ux system and would like to find a way to trap any errors that occur within the script.  If an oracle error would occur, in the script execution, we would like to know of its success or failure when the script is finished.&lt;BR /&gt;&lt;BR /&gt;Can anyone offer any suggestions?&lt;BR /&gt;&lt;BR /&gt;Thanks in advance,&lt;BR /&gt;Randy</description>
      <pubDate>Fri, 02 May 2003 14:31:11 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/how-to-trap-errors-in-pl-sql-script/m-p/2964736#M814871</guid>
      <dc:creator>Randy Hagedorn</dc:creator>
      <dc:date>2003-05-02T14:31:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to trap errors in pl/sql script?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/how-to-trap-errors-in-pl-sql-script/m-p/2964737#M814872</link>
      <description>hi,&lt;BR /&gt;&lt;BR /&gt;You have to put your exception blocks around the statements that can cause exceptions, for example:&lt;BR /&gt;&lt;BR /&gt;    begin&lt;BR /&gt;       insert into t values ( ..... );&lt;BR /&gt;       exception&lt;BR /&gt;         when others LOG_ERROR;&lt;BR /&gt;    end;&lt;BR /&gt;&lt;BR /&gt;    begin&lt;BR /&gt;        update y set ....;&lt;BR /&gt;        exception&lt;BR /&gt;          when others LOG_ERROR;&lt;BR /&gt;    end;&lt;BR /&gt;&lt;BR /&gt;where LOG_ERROR and supporting routines would look like:&lt;BR /&gt;================================================================&lt;BR /&gt;create or replace function whence return varchar2&lt;BR /&gt;as&lt;BR /&gt; call_stack  varchar2(4096) default dbms_utility.format_call_stack || chr(10);&lt;BR /&gt; n           number;&lt;BR /&gt; found_stack BOOLEAN default FALSE;&lt;BR /&gt; line        varchar2(255);&lt;BR /&gt; cnt         number := 0;&lt;BR /&gt;begin&lt;BR /&gt; loop&lt;BR /&gt;   n := instr( call_stack, chr(10) );&lt;BR /&gt;   exit when ( cnt = 4 or n is NULL or n = 0 );&lt;BR /&gt;&lt;BR /&gt;   line := substr( call_stack, 1, n-1 );&lt;BR /&gt;   call_stack := substr( call_stack, n+1 );&lt;BR /&gt;   if ( NOT found_stack ) then&lt;BR /&gt;     if ( line like '%handle%number%name%' ) then&lt;BR /&gt;      found_stack := TRUE;&lt;BR /&gt;     end if;&lt;BR /&gt;   else&lt;BR /&gt;     cnt := cnt + 1;&lt;BR /&gt;   end if;&lt;BR /&gt; end loop;&lt;BR /&gt; return line;&lt;BR /&gt;end;&lt;BR /&gt;/&lt;BR /&gt;&lt;BR /&gt;set define on&lt;BR /&gt;create table error_table&lt;BR /&gt;  ( timestamp  date,&lt;BR /&gt;    whence     varchar2(1024),&lt;BR /&gt;    msg        varchar2(1024),&lt;BR /&gt;    code       number&lt;BR /&gt;  )&lt;BR /&gt;/&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;create or replace procedure log_error&lt;BR /&gt;as&lt;BR /&gt;  pragma autonomous_transaction;&lt;BR /&gt;&lt;BR /&gt;  l_whence varchar2(1024);&lt;BR /&gt;  l_msg    varchar2(1020) default sqlerrm;&lt;BR /&gt;  l_code   number default sqlcode;&lt;BR /&gt;begin&lt;BR /&gt;  l_whence := whence;&lt;BR /&gt;  insert into error_table&lt;BR /&gt;    ( timestamp, whence, msg, code )&lt;BR /&gt;  values&lt;BR /&gt;    ( sysdate, whence, l_msg, l_code );&lt;BR /&gt;&lt;BR /&gt;  commit;&lt;BR /&gt;  exception&lt;BR /&gt;   when others then&lt;BR /&gt;     rollback;&lt;BR /&gt;     raise;&lt;BR /&gt;end;&lt;BR /&gt;/&lt;BR /&gt;================================================================&lt;BR /&gt;&lt;BR /&gt;Now let us test:&lt;BR /&gt;&lt;BR /&gt;yd@MYDB.MU&amp;gt; create table t ( x int primary key );&lt;BR /&gt;&lt;BR /&gt;Table created.&lt;BR /&gt;&lt;BR /&gt;Elapsed: 00:00:00.12&lt;BR /&gt;yd@MYDB.MU&amp;gt; begin&lt;BR /&gt; begin&lt;BR /&gt;  insert into t values ( 1 );&lt;BR /&gt;  exception&lt;BR /&gt;   when others then log_error;&lt;BR /&gt;    raise;&lt;BR /&gt; end;&lt;BR /&gt; begin&lt;BR /&gt;   insert into t values ( 1 );&lt;BR /&gt;   exception&lt;BR /&gt;    when others then log_error;&lt;BR /&gt;     raise;&lt;BR /&gt; end;&lt;BR /&gt;end;&lt;BR /&gt;/ &lt;BR /&gt;begin&lt;BR /&gt;*&lt;BR /&gt;ERROR at line 1:&lt;BR /&gt;ORA-00001: unique constraint (YD.SYS_C0010813) violated&lt;BR /&gt;ORA-06512: at line 12&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Elapsed: 00:00:00.53&lt;BR /&gt;&lt;BR /&gt;Now, display the results of the information that has been logged:&lt;BR /&gt;=====================================================================&lt;BR /&gt;yd@MYDB.MU&amp;gt; exec print_table('select * from error_table');&lt;BR /&gt;TIMESTAMP        : 03-may-2003 09:14:43&lt;BR /&gt;WHENCE           : c0000000249a5988        11  anonymous block&lt;BR /&gt;MSG              : ORA-00001: unique constraint (YD.SYS_C0010813) violated&lt;BR /&gt;CODE             : -1&lt;BR /&gt;-----------------&lt;BR /&gt;&lt;BR /&gt;PL/SQL procedure successfully completed.&lt;BR /&gt;&lt;BR /&gt;Elapsed: 00:00:00.49&lt;BR /&gt;yd@MYDB.MU&amp;gt; &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Now if you want, you can write a piece of code that will send this error message to a Mail recipient using SMTP_MAIL package. Instead of the user having to come to verify this table for possibilities of errors.&lt;BR /&gt;&lt;BR /&gt;For showing success, you may wish to modify this piece of code to include you own comments for success, and again define your own notification mechanism.&lt;BR /&gt;&lt;BR /&gt;Hope this helps!&lt;BR /&gt;&lt;BR /&gt;Best regards&lt;BR /&gt;Yogeeraj</description>
      <pubDate>Sat, 03 May 2003 04:23:20 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/how-to-trap-errors-in-pl-sql-script/m-p/2964737#M814872</guid>
      <dc:creator>Yogeeraj_1</dc:creator>
      <dc:date>2003-05-03T04:23:20Z</dc:date>
    </item>
  </channel>
</rss>

