what situation we can go for pragma_exception_init - oracle11g

Can anybody explain why we need pragma exception init. we have user defined exception which does the same job as pragma. Your answer will be appreciated.

pragma exception_init is used to provide a name to an exception so that you can reference the exception by that name in the exception handler. Only a few of the several thousand Oracle exceptions have predefined names. You can use exception_init to create names for these other exceptions.
see https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/errors.htm#BABGIIBI
I am editing my previous answer because I can insert formatted text here.
In your example, you are defining an exception that you are throwing, but what if you want to catch an existing exception that the system is throwing, but is unnamed? In the example that follows, I handle two specific errors 'DATA_NOT_FOUND', and ORA-600, which is a system error (and yes, I have some code that gets these).
CREATE OR REPLACE PROCEDURE exception_init_demo
AS
l_routine VARCHAR2 (30) := $$plsql_unit;
severe_system_error EXCEPTION;
PRAGMA EXCEPTION_INIT (severe_system_error, -600);
BEGIN
-- Execution code goes here
NULL;
EXCEPTION
WHEN severe_system_error
THEN
-- Severe error, log it, notify the dba, re-raise the exception
log_error (
p_application => $$plsql_unit
, p_routine => l_routine
, p_message => SQLERRM || UTL_TCP.crlf || DBMS_UTILITY.format_error_backtrace ()
);
notify_dba;
RAISE;
WHEN NO_DATA_FOUND
THEN
-- No data was found, this is OK, ignore error and return
return;
WHEN OTHERS
THEN
-- all other errors we will log and re-raise
log_error (
p_application => $$plsql_unit
, p_routine => l_routine
, p_message => SQLERRM || UTL_TCP.crlf || DBMS_UTILITY.format_error_backtrace ()
);
RAISE;
END;

Related

How to remove extra information form error message after executing a package body?

I want to remove extra information form error message after executing a package body.
Example:
If I run a anonymous block then the error message come like -
ERROR at line 1:
ORA-20010: Object ID PE556092 is not Produced by the Template TP000036
ORA-06512: at "EXAMPLE.TESTPROC", line 5
ORA-06512: at line 1
But i want to show only the below error message:
ORA-20010: Object ID PE556092 is not Produced by the Template TP000036
You can use the below to get the Error Code and Message respectively:
dbms_output.put_line('Error code: '||sqlcode);
dbms_output.put_line('Error msg: '||sqlerrm);
Read more about SQLERRM.
You can do that by defining an exception and mapping it to the SQLCODE of the raised error. Then you handle the logic in the exception handler.
Example, here I map to error -20010 (the one you are using):
declare
err_obj_not_produced exception;
pragma exception_init (err_obj_not_produced, -20010);
l_object varchar2 (50);
l_template varchar2 (50);
begin
run_your_code ('With values');
exception
when err_obj_not_produced then
return 'Object ID ' || l_object || ' is not Produced by the Template ' || l_template;
end;
Another one way to hide information about previous errors. Suppose you have two procedures:
create or replace procedure proc1 is
begin
raise no_data_found;
end;
/
create or replace procedure proc2 is
begin
proc1;
exception
when no_data_found then
raise_application_error(-20010, 'There is no spoon, Neo.');
end;
/
when you run a block in SQL*Plus, you get:
SQL> begin
proc2;
end;
/
begin
*
ERROR at line 1:
ORA-20010: There is no spoon, Neo.
ORA-06512: at "DEV.PROC2", line 6
ORA-06512: at line 2
You can change proc2 like this:
create or replace procedure proc2 is
begin
proc1;
end;
/
And call it like this with desired result:
SQL> begin
begin
proc2;
exception
when no_data_found then
raise_application_error(-20010, 'There is no spoon, Neo.', false);
end;
exception
when others then
dbms_output.put_line(SQLERRM);
end;
/
ORA-20010: There is no spoon, Neo.
PL/SQL procedure successfully completed.
This example shows how third parameter of raise_application_error procedure works. If you pass false, procedure deletes information about previously raised exceptions from error stack. It also will be impossible to get this information using dbms_utility.format_error_backtrace.
As Jon Heller noticed in comments, hiding information about exceptions is a bad practice (and using when others then without raise too, as Jeffry Kemp noticed), so, I hope you know what you are doing. This way or another, it is a shooting yourself in the foot.

Exception handling - plsql

I have a problem. Am doing an insert function
case f1.RFD_CATEGORY_CODE when'O1' then 'C1GBC'
when 'O2' then 'C2GBC' else null end
the field is mandatory and thus instead of null i need to show an error message if the code is not taking C1GBC or C2GBC. and if the code is taking C1GBC or C2GBC then show successful as message.
i have create an exception below but am getting error
create or replace procedure CTP_CODE as
declare
--RFD_CAT_ERR varchar2;
RFD_CAT_ERR EXCEPTION;
begin
if RFD_CATEGORY_CODE is '01' then RFD_CATEGORY_CODE is 'C1GBC';
DBMS_OUTPUT.PUT_LINE ('No1. Successful Operation');
else
if RFD_CATEGORY_CODE is '02' then RFD_CATEGORY_CODE is 'C2GBC';
DBMS_OUTPUT.PUT_LINE ('No2. Successful Operation');
end if;
raise RFD_CAT_ERR;
end if;
EXCEPTION
when RFD_CAT_ERR then
DBMS_OUTPUT.PUT_LINE ('Error message!');
end;
/
Wrong Code also wrong syntax..
Whenever your code goes in else condition it will throw error as after executing 2nd if it will go to "raise RFD_CAT_ERR;" part and raise an exception.
so u should handle error in elsif after 2nd If condition.
You say the field is mandatory. If by that you mean the field in the table is constrained as not null then you don't have to worry about raising an exception. The insert statement will do that for you. All you have to do is catch it in your exception handler and use RAISE_APPLICATION_ERROR to return a meaningful message.
Do that rather than try to print an error message. If your procedure is called by a batch process, there will be no one there to see it.
This works for me :D
CASE f1.RFD_CATEGORY_CODE
WHEN 'O1' THEN 'C1GBC'
WHEN 'O2' THEN 'C2GBC'
ELSE 'error'
Then raise any errors for exception

Display custom message when constraint is violated PL/SQL

I'm writing a pl/sql procedure. I've got a constraint on a column which doesn't allow values below 0 and above 500. I need to display a custom message if this constraint is violated (eg. "ID out of range"). Currently this is the exception and also the output in getting. There is another procedure that is outputting the error, hence the use raise_applcation_error.
Exception
when VALUE_ERROR then
raise_application_error(-20002, 'Customer ID out of range');
Error Message
"ORA-20000: ORA-02290: check constraint (s5849497.CK_ID_RANGE) violated"
What I would like
"ORA-20000: Customer ID out or range"
Here is the whole block if it helps
set serveroutput on;
---------------------------------------------------------------
alter table customer
add constraint ck_id_range
check (custid > 0 and custid < 500);
---------------------------------------------------------------
create or replace procedure ADD_CUSTOMER_TO_DB(pcustid number, pcustname varchar2) as
begin
insert into customer
values (pcustid,pcustname,0,'OK');
exception
when DUP_VAL_ON_INDEX then
raise_application_error(-20001, 'Duplicate customer ID');
when VALUE_ERROR then
raise_application_error(-20002, 'Customer ID out of range');
when others then
raise_application_error(-20000, SQLERRM);
end;
Thank you
I'm guessing that you're getting an ORA-02290 error. The way to catch this in an exception handler is to declare and initialize an exception for the particular error code (in this case, -2290) and then use your custom exception in the handler:
create or replace procedure ADD_CUSTOMER_TO_DB(pcustid number,
pcustname varchar2)
as
eCheck_constraint_violated EXCEPTION;
PRAGMA EXCEPTION_INIT(eCheck_constraint_violated, -2290);
begin
insert into customer
values (pcustid,pcustname,0,'OK');
exception
when DUP_VAL_ON_INDEX then
raise_application_error(-20001, 'Duplicate customer ID');
when eCheck_constraint_violated then
raise_application_error(-20002, 'Customer ID out of range');
when others then
raise_application_error(-20000, SQLERRM);
end;
As you'll see above, I just replaced VALUE_ERROR with the newly-defined exception.
If by chance the error you're getting is not -2290, just put the error you're seeing in PRAGMA EXCEPTION_INIT invocation above instead of -2290.
Share and enjoy.

Retrieving issues from jira conditoinally

I am using a sql http request to retrieve issues from JIRA , so far I can retrieve chosen number of issues according to the assignee name or reporter.
My problem now that I can not retrieve issues according to the creation field (date when the issues has been created) or othe custom field, I am receiving error :
Unrecognized field !
My approach was to play around with this part of the code :
**lv_json_request := '{'
||'"jql": "assignee='||:P9_ASSIGNEE||'",'
||'"startAt": '||NVL(:P9_STARTAT,0)||','
||'"maxResults": '||:P9_MAXRESULTS
||'}';**
You can find below the whole pl/sql block , it works fine with the current situation .
DECLARE
http_req utl_http.req;
http_resp utl_http.resp;
lv_json_request VARCHAR2(32767);
lc_response CLOB;
lv_response VARCHAR2(32767);
BEGIN
lv_json_request := '{'
||'"jql": "assignee='||:P9_ASSIGNEE||'",'
||'"startAt": '||NVL(:P9_STARTAT,0)||','
||'"maxResults": '||:P9_MAXRESULTS
||'}';
UTL_HTTP.set_wallet('file:/oracle/ora11/owm/wallets/oracle', 'apex4wallet');
http_req:= utl_http.begin_request
( url => 'https://rb-wam.bosch.com/tracker/rest/api/2/search'
, method => 'POST'
);
utl_http.set_header(http_req, 'Authorization', 'Basic '||:F_JIRA_TOKEN_REST);
utl_http.set_header(http_req, 'Content-Type', 'application/json');
utl_http.set_header(http_req, 'Content-Length', LENGTH(lv_json_request));
utl_http.write_text(http_req, lv_json_request);
http_resp:= utl_http.get_response(http_req);
-- read data from response
BEGIN
LOOP
utl_http.read_text(http_resp, lv_response);
HTP.PRN(lv_response);
lc_response := lc_response || TO_CLOB(lv_response);
END LOOP;
EXCEPTION
WHEN UTL_HTTP.END_OF_BODY THEN
utl_http.end_response(http_resp);
END;
-- log details
--DELETE webservice_log;
INSERT INTO webservice_log (seq_id,clob_response,clob_request) VALUES (sqe_Webservice_Log.NEXTVAL,lc_response,TO_CLOB(lv_json_request));
--HTP.P(lc_response);
EXCEPTION
WHEN OTHERS THEN
RAISE;
END;
You can add something like this to your lv_json_request statement:
"createdDate<='||:P9_CREATED||'"
using any valid operator instead of "<=", depending on your needs.

Catching a constraint violation in psql

I am using sql developer, and have added a constraint to one of my tables.
constraint valid_gender check(gender in ('M','F','I','T'))
When I try to add an entry with say 'x' for gender using a plsql procedure, it fails with constraint violation (as it should).
I want to add a "Catch" to the plsql procedure so that if valid_gender is voilated I can raise_application_error specific to it. Is this possible?
Oracle will raise an exception that says:
ORA-02290: check constraint (yourschema.valid_gender) violated
You can catch that in an exception handler and raise your own exception instead using raise_application_error in a couple of ways.
1) You can specifically trap the ORA-02290 exception like this:
declare
e_check_violated exception
pragma exception_init (e_check_violated, -2290);
begin
insert ...
exception
when e_check_violated then
if sqlerrm like '%(yourschema.valid_gender)%' then
raise_application_error(-20001,'Invalid gender');
else
raise;
end if;
end;
2) You can trap all exceptions and inspect them:
begin
insert ...
exception
when others then
if sqlerrm like 'ORA-02290:%(yourschema.valid_gender)%' then
raise_application_error(-20001,'Invalid gender');
else
raise;
end if;
end;
In a large application it is quite common to have an exception handling procedure to generalise this and look up the constraint-specific message in a table.
use anonym block in your code...
BEGIN
INSERT or update...
EXCEPTION
WHEN dup_val_on_index THEN
RISE...
END;
You could just test it first:
if gender_value not in ('M','F','I','T') then
raise_application_error...
end if;

Resources