When to re-raise the same exception in Oracle - oracle

I'm reading Steven Feuerstein's PL/SQL book. According to it:
Use this form when you want to re-raise (or propagate out) the same
exception from within an exception handler, as you see here:
EXCEPTION
WHEN NO_DATA_FOUND
THEN
-- Use common package to record all the "context" information,
-- such as error code, program name, etc.
errlog.putline (company_id_in);
-- And now propagate NO_DATA_FOUND unhandled to the enclosing block.
RAISE;
This feature is useful when you want to log the fact that an error
occurred, but then pass that same error out to the enclosing block.
That way, you record where the error occurred in your application but
still stop the enclosing block(s) without losing the error
information.
I give it a try:
create table log_error
(
error_code number,
error_name varchar2(400)
);
declare
l_q number := 400;
l_r number := 0;
l_result number;
err_num NUMBER;
err_msg VARCHAR2(100);
begin
l_result := l_q/l_r;
EXCEPTION
WHEN OTHERS THEN
err_num := SQLCODE;
err_msg := SUBSTR(SQLERRM, 1, 100);
insert into log_error values (err_num , err_msg);
end;
select * from log_error;
I got below data in my log_error table:
-1476 ORA-01476: divisor is equal to zero
Now I place raise in my exception block:
declare
l_q number := 400;
l_r number := 0;
l_result number;
err_num NUMBER;
err_msg VARCHAR2(100);
begin
l_result := l_q/l_r;
EXCEPTION
WHEN OTHERS THEN
err_num := SQLCODE;
err_msg := SUBSTR(SQLERRM, 1, 100);
insert into log_error values (err_num , err_msg);
raise;
end;
Now when I run this block, i got nothing in my log table and also I got the error.
Error report -
ORA-01476: divisor is equal to zero
ORA-06512: at line 14
01476. 00000 - "divisor is equal to zero"
What is the use of raise? When I have to use this?

Your logging process needs to be a little different. When an exception is called all data/transactions that are not committed are rolled back. You can add more details with the newer features Oracle has added.
err_msg := DBMS_UTILITY.FORMAT_ERROR_STACK()||DBMS_UTILITY.FORMAT_ERROR_BACKTRACE();
You need to create a logging procedure which uses the PRAGMA AUTONOMOUS TRANSACTION. Pass in the SQLCODE and err_msg with the details and this will log the error no matter what. Here is what I use which also uses Feurstein's Q Error package. This link to the Q$Error package is quite informative.
PROCEDURE LOG (err_in IN INTEGER:= SQLCODE,
msg_in IN VARCHAR2:= NULL,
vlocation_in IN VARCHAR2:= NULL)
IS
/******************************************************************************
PURPOSE: log a code error, business logic error or information message in APPLICATION_ERROR_LOGGING
we want the error message to be logged even if the calling transaction fails or hangs
******************************************************************************/
PRAGMA AUTONOMOUS_TRANSACTION;
v_err_text VARCHAR2 (4000) := SQLERRM;
BEGIN
v_err_text := v_err_text || ' ' || GET_MORE_ERROR_DESCRIPTION (err_in);
INSERT INTO application_error_logging (ID,
request_uri,
ERROR_CODE,
user_id,
stack_trace,
information,
"TIMESTAMP")
VALUES (application_error_logging_seq.NEXTVAL,
vlocation_in,
TO_CHAR (err_in),
g_admin_id,
msg_in,
v_err_text,
localtimestamp);
COMMIT;
EXCEPTION
WHEN OTHERS
THEN
q$error_manager.raise_error (
error_code_in => SQLCODE,
text_in => SQLERRM,
name1_in => 'LOCATION',
value1_in => 'APP_UTIL.LOG',
name2_in => 'v_location',
value2_in => vlocation_in,
name3_in => 'err_in',
value3_in => TO_CHAR (err_in)
);
END LOG;

Apparently, Steven Feuerstein's procedure errlog.putline() uses an autonomous transaction to insert the record into the log table. In your case you perform the insert in the same transaction, which is rolled back by the caller when the exception is re-raised.

Related

Ignore lines that causes errors

I have a big Oracle script with thousands of package call inside a BEGIN - END;
Is there a way to ignore the lines that causes error and continue executing the next lines? Some sort of "On Error Resume Next" in vb.
If you have only one BEGIN END section, then you can use EXCEPTION WHEN OTHERS THEN NULL.
SQL> declare
v_var pls_integer;
begin
select 1 into v_var from dual;
-- now error
select 'A' into v_var from dual;
exception when others then null;
end;
SQL> /
PL/SQL procedure successfully completed.
SQL> declare
v_var pls_integer;
begin
select 1 into v_var from dual;
-- now error
select 'A' into v_var from dual;
--exception when others then null;
end;
/
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 6
SQL>
The whole concept of "ignore errors" is a bug, and a lie if any errors occur. That is not to say you cannot trap errors and continue processing, just that you MUST handle the errors. For example, assume the use case: "Data has been loaded into a stage table from multiple .csv files. Now load into the tables A and Table B according to ....".
create procedure
Load_Tables_A_B_from_Stage(process_message out varchar2)
is
Begin
For rec in (select * from stage)
loop
begin
insert into table_a (col1, col2)
values (rec.col_a1, col_a2);
insert into table_b (col1, col2)
values (rec.col_b1, col_b2);
exception
when others then null;
end;
end loop;
process_message := 'Load Tables A,B Complete';
end ;
Now suppose a user created the a .csv file entered "n/a" in numeric columns where there was no value or the value was unknown. The result of this all too common occurrence is all such rows were not loaded, but you have no way to know that until the user complains their data was not loaded even though you told them it was. Further you have no way of determining the problem.
A much better approach is to "capture and report".
create procedure
Load_Tables_A_B_from_Stage(process_message out varchar2)
is
load_error_occurred boolean := False;
Begin
For rec in (select * from stage)
loop
begin
insert into table_a (col1, col2)
values (rec.col_a1, rec.col_a2);
exception
when others then
log_load_error('Load_Tables_A_B_from_Stage', stage_id, sqlerrm);
load_error_occurred := True;
end;
begin
insert into table_b (col1, col2)
values (rec.col_b1, rec.col_b2);
exception
when others then
log_load_error('Load_Tables_A_B_from_Stage', stage_id, sqlerrm);
load_error_occurred := True;
end;
end loop;
if load_error_occurred then
process_message := 'Load Tables A,B Complete: Error(s) Detected';
else
process_message := 'Load Tables A,B Complete: Successful No Error(s)';
end if;
end Load_Tables_A_B_from_Stage ;
Now you have informed the user of the actual status, and where you are contacted you can readily identify the issue.
User here is used in the most general sense. It could mean a calling routine instead of an individual. Point is you do not have to terminate your process due to errors but DO NOT ignore them.
I don't think there is any magic one-liner that will solve this.
As others have, use a editor to automate the wrapping of each call within a BEGIN-EXCEPTION-END block might be quicker/easier.
But, if feel a little adventurous, or try this strategy:
Let's assume you have this:
BEGIN
proc1;
proc2;
proc3;
.
.
.
proc1000;
END;
You could try this (untested, uncompiled but might give you an idea of what to try):
DECLARE
l_progress NUMBER := 0;
l_proc_no NUMBER := 0;
e_proc_err EXCEPTION;
-- A 'runner' procedure than manegrs the counters and runs/skips dpending on these vals
PROCEDURE run_proc ( pname IN VARCHAR2 ) IS
BEGIN
l_proc_no := l_proc_no + 1;
IF l_proc_no >= l_progress
THEN
-- log 'Running pname'
EXECUTE IMMEDIATE 'BEGIN ' || pname || '; END;' ;
l_progress := l_progress + 1;
ELSE
-- log 'Skipping pname'
END IF;
EXCEPTION
WHEN OTHERS THEN
-- log 'Error in pname'
l_progress := l_progress + 1;
RAISE e_proc_err;
END;
BEGIN
l_progress := 0;
<<start>>
l_proc_no := 0;
run_proc ( 'proc1' );
run_proc ( 'proc2' );
run_proc ( 'proc3' );
.
.
run_proc ( 'proc1000' );
EXCEPTION
WHEN e_proc_err THEN
GOTO start;
WHEN OTHERS THEN
RAISE;
END;
The idea here is to add a 'runner' procedure to execute each procedure dynamically and log the run, skip, error.
We maintain a global count of the current process number (l_proc_no) and overall count of steps executed (l_progress).
When an error occurs we log it, raise it and let it fall into the outer blocks EXCEPTION handler where it will restart via an (evil) GOTO.
The GOTO is placed such that the overall execution count is unchanged but the process number is reset to 0.
Now when the run_proc is called it sees that l_progress is greater than l_proc_no, and skips it.
Why is this better than simply wrapping a BEGIN EXCEPTION END around each call?
It might not be, but you make a smaller change to each line of code, and you standardise the logging around each call more neatly.
The danger is a potential infinite loop which is why I specify e_proc_err to denote errors within the called procedures. But it might need tweaking to make it robust.

Oracle full error message in log

I'm trying to get the full error message from oracle.
For example - I have a very long procedure that doing a lot of manipulation on
a lot of objects, and in my log I got the error
object no longer exist.
And this is my insert to the log (even it is a generally question - not specific to this example):
EXCEPTION WHEN OTHERS THEN
v_errno := sqlcode;
V_ERRMSG := SQLERRM;
INSERT INTO ERR_TABLE (ERROR_NUMBER, ERROR_MESSAGE,PROGRAM#)
VALUES (V_ERRNO, V_ERRMSG,'MY_PKG');
COMMIT;
The problem is that I don't know which table it talking about - because this
information doesn't exsits.
Is there a way to get it?
I guess that oracle save it in some place.
thanks!
For internal logging (not only for errors) I use a procedure like this:
PROCEDURE Put(
LogMessage IN T_LOG_ENTRIES.LOG_MESSAGE%TYPE,
ErrCode IN T_LOG_ENTRIES.LOG_ERROR_CODE%TYPE DEFAULT 0) IS
ErrorStack T_LOG_ENTRIES.LOG_ERROR_STACK%TYPE;
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
IF ErrCode <> 0 THEN
ErrorStack := DBMS_UTILITY.FORMAT_ERROR_BACKTRACE();
ErrorStack := SQLERRM(ErrCode) || CHR(13) || ErrorStack;
END IF;
INSERT INTO T_LOG_ENTRIES
(LOG_DATE, LOG_MESSAGE, LOG_ERROR_CODE, LOG_ERROR_STACK)
VALUES
(CURRENT_TIMESTAMP, LogMessage, ErrCode, ErrorStack);
COMMIT;
END Put;
DBMS_UTILITY.FORMAT_ERROR_BACKTRACE() provides the full error stack. You should use AUTONOMOUS_TRANSACTION since transactions are rolled back in case of exception, i.e. your log message would be deleted also.
Then you can use the procedure for example as this:
BEGIN
...
EXCEPTION WHEN OTHERS THEN
Put('Error in my procedure', sqlcode);
END;

Catch ORA exceptions in PL/SQL

I wrote a package to add records in a country table that has a reference key pointing to a "regions" table using region_id.So,if I try to add a "region_id" in my countries table and if that value does not exist in my regions table,I should throw the exception and catch.
My package code is:
CREATE PACKAGE BODY cus7 AS
v_error_code NUMBER;
region_exists pls_integer;
procedure addi6 (c_cntry_id in out countries.country_id%type,
c_cntr_name in countries.country_name%type,
c_rgn_id in countries.region_id%type)
is
begin
begin
select 1 into region_exists
from regions r
where r.region_id = c_rgn_id;
exception
when no_data_found then
region_exists := 0;
DBMS_OUTPUT.PUT_LINE('Region not present');
end;
if region_exists = 1 then
insert into countries(country_id, country_name,region_id)
values (c_cntry_id, c_cntr_name, c_rgn_id);
DBMS_OUTPUT.PUT_LINE('Inserted');
END IF;
EXCEPTION
WHEN dup_val_on_index
THEN
c_cntry_id := null;
DBMS_OUTPUT.PUT_LINE('Already present');
end addi6;
END cus7;
/
Now,in my procedure,everything is working fine,except when I do an add like this:
DECLARE
outputValue CHAR(2) := 'KO';
begin
cus7.addi6(outputValue,'KOREA',5);
end;
/
apart from getting my own message which is "Region not found",I am also getting ORA-01403-No data found.
My question is if there is a way to catch this ORA exception or avoid display?
Tx in advance
Yes, all you have to do is add WHEN OTHERS to your exception block in order to catch all of the other possible ORA exceptions.
EXCEPTION
WHEN dup_val_on_index
THEN
c_cntry_id := null;
DBMS_OUTPUT.PUT_LINE('Already present')
WHEN OTHERS
THEN
DBMS_OUTPUT.PUT_LINE('Another Error');
-- other logic here
Here is an extensive documentation regarding error handling in PL/SQL.

plsql stored procedure syntax error

when I try the following code, I get a procedure completed with 0 compilation errors.. message instead of procedure successfully completed message.
what's wrong with this? and help me in correcting this error
CREATE OR REPLACE PROCEDURE omar_manager_report1 (
pi_co_id IN VARCHAR2, -- Company ID
pi_cntr_nbr IN NUMBER DEFAULT 0,
-- if Contract number is passed then Case 1: will be executed
pi_overdue_days IN NUMBER DEFAULT 0,
--No of days related to Over due of application passed.
po_var_ref OUT sys_refcursor
)
IS
lv_query VARCHAR2(400) ;
lv_co_id VARCHAR2 (200);
BEGIN
lv_co_id := REPLACE (pi_co_id, ',', ''',''');
--Default option for all group office
lv_query :=
'select distinct gue.co_id,
(SELECT event_descp FROM get_event WHERE co_id = gue.co_id AND event_cd = gue.event_cd) AS event_desc
FROM get_uwtg_event gue';
DBMS_OUTPUT.put_line ('lv_query');
OPEN po_var_ref FOR lv_query;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (SQLCODE);
END;
/
Executing a CREATE OR REPLACE PROCEDURE... statement compiles the procedure, but it does not call the procedure. You'll need to write a PL/SQL block similar to the following to invoke your procedure:
DECLARE
csrOut SYS_REFCURSOR;
BEGIN
omar_manager_report1(pi_co_id => '123456',
pi_cntr_nbr => 0,
pi_overdue_days => 0,
po_var_ref => csrOut);
-- Add code here to fetch from csrOut and use the results appropriately
-- Close the cursor opened by omar_manager_report1
CLOSE csrOut;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Exception: ' || SQLCODE || ' : ' || SQLERRM);
END;
Best of luck.

ora-01422 error by a procedure

Below code throwing ORA-01422 error. As my code uses select ... into I come to know it is fetching more than one row from the table but how can I overcome this by eliminating select into statement. Here is the code:
PROCEDURE Call_Transaction ( Transaction_Name Varchar2, Transaction_Type Varchar2, Form_Open_Type Varchar2 ) IS
BEGIN
Declare
M_Transaction_Name U_Transaction_Master.Transaction_Name%Type := Upper(Transaction_Name);
M_Transaction_Cd U_Transaction_Master.Transaction_Cd%Type;
T_Transaction_Cd U_Transaction_Master.Transaction_Cd%Type;
Begin
Select Transaction_Cd Into M_Transaction_Cd From U_Transaction_Master
Where Transaction_Name = M_Transaction_Name ;
Begin
Select Transaction_Cd Into T_Transaction_Cd From U_User_Wise_Transactions
Where Login_Cd = :Global.Login_Cd And Transaction_Cd = M_Transaction_Cd And
Inst_Cd = :Global.Company_Cd And
To_Char(Valid_Upto_Date,'DD-MM-YYYY') = '31-12-9999';
If Transaction_Type = 'FORM' And Upper(Form_Open_Type) = 'CALL_FORM' Then
DECLARE
id FormModule;
BEGIN
id := Find_Form(M_Transaction_Name); --<Replace your form name>--
IF Id_Null(id) THEN
Call_Form(:Global.Forms_Path||M_Transaction_Name||'.Fmx');
ELSE
Go_Form(Id) ;
END IF ;
END ;
Elsif Transaction_Type = 'FORM' And Upper(Form_Open_Type) = 'OPEN_FORM' Then
Open_Form(:Global.Forms_Path||M_Transaction_Name||'.Fmx');
Elsif Transaction_Type = 'REPORT' And Upper(Form_Open_Type) = 'RUN_PRODUCT' Then
Declare
Pl_Id ParamList;
Begin
Pl_Id := Get_Parameter_List('tmpdata');
IF NOT Id_Null(Pl_Id) THEN
Destroy_Parameter_List( Pl_Id );
END IF;
Pl_Id := Create_Parameter_List('tmpdata');
ADD_Parameter(pl_id,'Inst_Cd',TEXT_PARAMETER,:GLOBAL.Company_Cd);
ADD_Parameter(pl_id,'Ac_Year_Cd',TEXT_PARAMETER,:GLOBAL.Ac_Year_Cd);
ADD_Parameter(Pl_Id,'INST_NAME',TEXT_PARAMETER, :Global.Company_name);
ADD_Parameter(Pl_Id,'ADDRESS',TEXT_PARAMETER, :Global.Address);
ADD_Parameter(pl_id,'FOOTER',TEXT_PARAMETER,:GLOBAL.Footer);
Run_Product(REPORTS,:Global.Reports_Path||M_Transaction_Name, SYNCHRONOUS, RUNTIME,
FILESYSTEM, Pl_Id, NULL);
End;
End If;
Exception
When No_Data_Found Then
Message('Sorry..., You Do Not Have Authorization For : '||M_Transaction_Cd||' Transaction Code...');
Raise Form_Trigger_Failure;
End;
Exception
When No_Data_Found Then
Message('The Transaction Cd Not Exists In Transaction Master, Please Contact Administrator...');
Raise Form_Trigger_Failure;
End;
END;
How can I rewrite the code to resolve ORA-01422 error?
First, you need to change exception handling logic:
enclose in begin ... exception ... end only part that really can
through exception;
handle too_many_rows exception
.
PROCEDURE Call_Transaction ( Transaction_Name Varchar2, Transaction_Type Varchar2, Form_Open_Type Varchar2 ) IS
BEGIN
Declare
M_Transaction_Name U_Transaction_Master.Transaction_Name%Type := Upper(Transaction_Name);
M_Transaction_Cd U_Transaction_Master.Transaction_Cd%Type;
T_Transaction_Cd U_Transaction_Master.Transaction_Cd%Type;
Begin
-- 1st select with error analysis
begin
Select Transaction_Cd Into M_Transaction_Cd From U_Transaction_Master
Where Transaction_Name = M_Transaction_Name ;
exception
when No_Data_Found then begin
Message('The Transaction Cd Not Exists In Transaction Master, Please Contact Administrator...');
Raise Form_Trigger_Failure;
end;
when too_many_rows then begin
-- What really must be done in this case?
Message('There are too many Transaction Cd's with passed name In Transaction Master, Please Contact Administrator...');
Raise Form_Trigger_Failure;
end;
end;
-- 2nd select with error analysis
begin
Select Transaction_Cd Into T_Transaction_Cd From U_User_Wise_Transactions
Where Login_Cd = :Global.Login_Cd And Transaction_Cd = M_Transaction_Cd And
Inst_Cd = :Global.Company_Cd And
To_Char(Valid_Upto_Date,'DD-MM-YYYY') = '31-12-9999';
Exception
When No_Data_Found Then begin
Message('Sorry..., You Do Not Have Authorization For : '||M_Transaction_Cd||' Transaction Code...');
Raise Form_Trigger_Failure;
end;
When too_many_rows Then begin
-- What really must be done in this case?
Message('Sorry..., there are some misconfiguration in Authorization Settings For : '||M_Transaction_Cd||' Transaction Code. Please contact Administrator.');
Raise Form_Trigger_Failure;
end;
End;
If Transaction_Type = 'FORM' And Upper(Form_Open_Type) = 'CALL_FORM' Then
---[... all other code skipped ...]---
End If;
END;
After refactoring you need to answer a question about what really must be performed in situations when more than one row found and handle it according to nature of implemented task.
If you worried about method that you can use to detect presence of values and determine it's count then you may look at this question on StackOverflow.
In oracle, you can keep the select into statement and limit the number of rows using ROWNUM:
Select Transaction_Cd Into M_Transaction_Cd From U_Transaction_Master
Where Transaction_Name = M_Transaction_Name
and ROWNUM < 2;

Resources