- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to trap errors in pl/sql script?
Operating System - HP-UX
1753768
Members
4853
Online
108799
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2003 07:31 AM
05-02-2003 07:31 AM
Hi,
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.
Can anyone offer any suggestions?
Thanks in advance,
Randy
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.
Can anyone offer any suggestions?
Thanks in advance,
Randy
Solved! Go to Solution.
1 REPLY 1
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2003 09:23 PM
05-02-2003 09:23 PM
Solution
hi,
You have to put your exception blocks around the statements that can cause exceptions, for example:
begin
insert into t values ( ..... );
exception
when others LOG_ERROR;
end;
begin
update y set ....;
exception
when others LOG_ERROR;
end;
where LOG_ERROR and supporting routines would look like:
================================================================
create or replace function whence return varchar2
as
call_stack varchar2(4096) default dbms_utility.format_call_stack || chr(10);
n number;
found_stack BOOLEAN default FALSE;
line varchar2(255);
cnt number := 0;
begin
loop
n := instr( call_stack, chr(10) );
exit when ( cnt = 4 or n is NULL or n = 0 );
line := substr( call_stack, 1, n-1 );
call_stack := substr( call_stack, n+1 );
if ( NOT found_stack ) then
if ( line like '%handle%number%name%' ) then
found_stack := TRUE;
end if;
else
cnt := cnt + 1;
end if;
end loop;
return line;
end;
/
set define on
create table error_table
( timestamp date,
whence varchar2(1024),
msg varchar2(1024),
code number
)
/
create or replace procedure log_error
as
pragma autonomous_transaction;
l_whence varchar2(1024);
l_msg varchar2(1020) default sqlerrm;
l_code number default sqlcode;
begin
l_whence := whence;
insert into error_table
( timestamp, whence, msg, code )
values
( sysdate, whence, l_msg, l_code );
commit;
exception
when others then
rollback;
raise;
end;
/
================================================================
Now let us test:
yd@MYDB.MU> create table t ( x int primary key );
Table created.
Elapsed: 00:00:00.12
yd@MYDB.MU> begin
begin
insert into t values ( 1 );
exception
when others then log_error;
raise;
end;
begin
insert into t values ( 1 );
exception
when others then log_error;
raise;
end;
end;
/
begin
*
ERROR at line 1:
ORA-00001: unique constraint (YD.SYS_C0010813) violated
ORA-06512: at line 12
Elapsed: 00:00:00.53
Now, display the results of the information that has been logged:
=====================================================================
yd@MYDB.MU> exec print_table('select * from error_table');
TIMESTAMP : 03-may-2003 09:14:43
WHENCE : c0000000249a5988 11 anonymous block
MSG : ORA-00001: unique constraint (YD.SYS_C0010813) violated
CODE : -1
-----------------
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.49
yd@MYDB.MU>
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.
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.
Hope this helps!
Best regards
Yogeeraj
You have to put your exception blocks around the statements that can cause exceptions, for example:
begin
insert into t values ( ..... );
exception
when others LOG_ERROR;
end;
begin
update y set ....;
exception
when others LOG_ERROR;
end;
where LOG_ERROR and supporting routines would look like:
================================================================
create or replace function whence return varchar2
as
call_stack varchar2(4096) default dbms_utility.format_call_stack || chr(10);
n number;
found_stack BOOLEAN default FALSE;
line varchar2(255);
cnt number := 0;
begin
loop
n := instr( call_stack, chr(10) );
exit when ( cnt = 4 or n is NULL or n = 0 );
line := substr( call_stack, 1, n-1 );
call_stack := substr( call_stack, n+1 );
if ( NOT found_stack ) then
if ( line like '%handle%number%name%' ) then
found_stack := TRUE;
end if;
else
cnt := cnt + 1;
end if;
end loop;
return line;
end;
/
set define on
create table error_table
( timestamp date,
whence varchar2(1024),
msg varchar2(1024),
code number
)
/
create or replace procedure log_error
as
pragma autonomous_transaction;
l_whence varchar2(1024);
l_msg varchar2(1020) default sqlerrm;
l_code number default sqlcode;
begin
l_whence := whence;
insert into error_table
( timestamp, whence, msg, code )
values
( sysdate, whence, l_msg, l_code );
commit;
exception
when others then
rollback;
raise;
end;
/
================================================================
Now let us test:
yd@MYDB.MU> create table t ( x int primary key );
Table created.
Elapsed: 00:00:00.12
yd@MYDB.MU> begin
begin
insert into t values ( 1 );
exception
when others then log_error;
raise;
end;
begin
insert into t values ( 1 );
exception
when others then log_error;
raise;
end;
end;
/
begin
*
ERROR at line 1:
ORA-00001: unique constraint (YD.SYS_C0010813) violated
ORA-06512: at line 12
Elapsed: 00:00:00.53
Now, display the results of the information that has been logged:
=====================================================================
yd@MYDB.MU> exec print_table('select * from error_table');
TIMESTAMP : 03-may-2003 09:14:43
WHENCE : c0000000249a5988 11 anonymous block
MSG : ORA-00001: unique constraint (YD.SYS_C0010813) violated
CODE : -1
-----------------
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.49
yd@MYDB.MU>
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.
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.
Hope this helps!
Best regards
Yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP