Catch ORA exceptions in PL/SQL - oracle

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.

Related

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;

PL/SQL no data found exception handling

I have a table EMAILS with columns: ROUTINE, EMAILS and COPIES defining to whom the result of particular procedure should be sent (as to/cc) using UTL_MAIL.
I have the following code:
PROCEDURE myproc AS
NO_EMAIL_FOUND EXCEPTION;
e EMAILS%ROWTYPE;
x NUMBER;
BEGIN
--fetch data
SELECT x INTO x FROM <...>;
--fetch emails
SELECT * INTO e FROM emails WHERE routine='FRS_WEEKLY';
IF e.emails IS NULL AND e.copies IS NULL THEN
RAISE NO_EMAIL_FOUND;
END IF;
<send mail code using UTL_MAIL>;
EXCEPTION
WHEN NO_EMAIL_FOUND THEN <code1>;
WHEN NO_DATA_FOUND THEN <code2>;
WHEN OTHERS THEN NULL; --RAISE;
END myproc;
I want myproc to execute code1 when there's no emails in EMAILS table. My problem is that NO_EMAIL_FOUND exception is not raised and code2 executed since NO_DATA_FOUND exception is raised before it in SELECT statement. How to avoid this?
Try this:
PROCEDURE myproc AS
NO_EMAIL_FOUND EXCEPTION;
e EMAILS%ROWTYPE;
x NUMBER;
BEGIN
BEGIN
--fetch data
SELECT x INTO x FROM <...>;
--fetch emails
BEGIN
SELECT * INTO e FROM emails WHERE routine='FRS_WEEKLY';
EXCEPTION WHEN NO_DATA_FOUND THEN
RAISE NO_EMAIL_FOUND;
END;
IF e.emails IS NULL AND e.copies IS NULL THEN
RAISE NO_EMAIL_FOUND;
END IF;
<send mail code using UTL_MAIL>;
EXCEPTION
WHEN NO_EMAIL_FOUND THEN <code1>;
WHEN NO_DATA_FOUND THEN <code2>;
WHEN OTHERS THEN NULL; --RAISE;
END;
END myproc;
Use this:
PROCEDURE myproc AS
NO_EMAIL_FOUND EXCEPTION;
e EMAILS%ROWTYPE;
x NUMBER;
BEGIN
--fetch data
SELECT x INTO x FROM <...>;
--fetch emails
BEGIN
SELECT * INTO e FROM emails WHERE routine='FRS_WEEKLY';
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE NO_EMAIL_FOUND;
END;
IF e.emails IS NULL AND e.copies IS NULL THEN
RAISE NO_EMAIL_FOUND;
END IF;
<send mail code using UTL_MAIL>;
EXCEPTION
WHEN NO_EMAIL_FOUND THEN <code1>;
WHEN NO_DATA_FOUND THEN <code2>;
WHEN OTHERS THEN NULL; --RAISE;
END myproc;
I hope you are aware that this routine can only process exactly one email; if there is more than one, a TOO_MANY_ROWS exception will be raised.

When to re-raise the same exception in 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.

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;

Handle ORACLE Exceptions

I need to handle the ORA-01400 error (cannot insert NULL into ("SCHEMA"."TABLE_NAME"."COLUMN_NAME") ) using a exception handle.
ORACLE Predefine a few Exceptions like (ACCESS_INTO_NULL, ZERO_DIVIDE and so on), but apparently does not define an Exception for the ORA-01400 error, how do I handle this particular error code?
I need something like this (other suggestions are accepted).
....
...
INSERT INTO MY_TABLE (CODE, NAME) VALUES (aCode,aName);
COMMIT;
EXCEPTION
WHEN NULL_VALUES THEN /* i don't know this value , exist?*/
Do_MyStuff();
WHEN OTHERS THEN
raise_application_error(SQLCODE,MY_OWN_FORMAT_EXCEPTION(SQLCODE,SQLERRM),TRUE);
END;
The pre-defined PL/SQL exceptions are special to Oracle. You really can't mess with those. When you want to have a set of predefined exceptions of your own you can't declare them "globally" like the standard ones. Instead, create an exceptions package which has all of the exception declarations and use that in your application code.
Example:
CREATE OR REPLACE PACKAGE my_exceptions
AS
insert_null_into_notnull EXCEPTION;
PRAGMA EXCEPTION_INIT(insert_null_into_notnull, -1400);
update_null_to_notnull EXCEPTION;
PRAGMA EXCEPTION_INIT(update_null_to_notnull, -1407);
END my_exceptions;
/
Now use the exception defined in the package
CREATE OR REPLACE PROCEDURE use_an_exception AS
BEGIN
-- application specific code ...
NULL;
EXCEPTION
WHEN my_exceptions.insert_null_into_notnull THEN
-- application specific handling for ORA-01400: cannot insert NULL into (%s)
RAISE;
END;
/
Source: http://www.orafaq.com/wiki/Exception
you can define your own exceptions, like variables (they will have the same scope as other variables so you can define package exception, etc...):
SQL> DECLARE
2 NULL_VALUES EXCEPTION;
3 PRAGMA EXCEPTION_INIT(NULL_VALUES, -1400);
4 BEGIN
5 INSERT INTO t VALUES (NULL);
6 EXCEPTION
7 WHEN null_values THEN
8 dbms_output.put_line('null value not authorized');
9 END;
10 /
null value not authorized
PL/SQL procedure successfully completed
You can handle exception by its code like this:
....
...
INSERT INTO MY_TABLE (CODE, NAME) VALUES (aCode,aName);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE = -1400 THEN
Do_MyStuff();
ELSE
raise_application_error(SQLCODE,MY_OWN_FORMAT_EXCEPTION(SQLCODE,SQLERRM),TRUE);
END IF;
END;
INSERT INTO MY_TABLE (CODE, NAME) VALUES (aCode,aName);
COMMIT;
EXCEPTION
WHEN NULL_VALUES /* i don't know this value , exist?*/
emesg := SQLERRM;
dbms_output.put_line(emesg);
WHEN OTHERS THEN
emesg := SQLERRM;
dbms_output.put_line(emesg);
END;
SQLERRM shows the sql error message
http://www.psoug.org/reference/exception_handling.html

Resources