Skip to main content

What is pragma exception_init in oracle sql plsql interview question and answer?

pragma exception_init :


It associates an exception name with oracle error number.ORA_error is handled by writing specific handler. Word pragma signifies or mean that it is compiler directive. Function SQLCODE is used to return the valid oracle error number.EXCEPTION_INT is used in the declarative part (before BEGIN keyword) of any PLSQL block, subprogram or package. Pragma must be appeared in the declaration section as it is associative exception which is used somewhere after the exception declaration. One exception name is assigned to the particular error number.

It is allowed to handle the specific errors using the custom exception names. It is directive used to handle the user defined exception with a specific error number. It is useful for mapping oracle error codes like ORA-00942, ORA-01400 to user defined exceptions which enables more readable and structured error which is going to handle in the code.

Error number in the future may arise conflict among the unrelated application using that number. Error code which is associated with exception error code can be either 100 which is the numeric code for no data found which SQL CODE function returns or any negative integer greater than -1000000 except -1403 which is another numeric code for no data found. No data found is the predefined exception.

If two exception EXCEPTION_INIT pragmas is to assign different error codes to the same user defined exception, then the later pragma overrides the earlier pragma. Negative integers greater than -65536 are only partially converted. Application declared exception which are raised and caught locally within the layer of entry don't need EXCEPTION_INIT pragmas. If it is unhandled beyond the layer of entry, then it automatically converts into ORA-6515.

An error is not having the predefined name, then this exception PRAGMA EXCEPTION_INIT is used to give the name.

SYNTAX:

PRAGMA EXCEPTION_INIT(exception_name,error_number);

Here,exception_name is the user defined exception name which you want to associate with an oracle error number & error_number which you want to associate with an exception.

create table emp3(empno int not null,sal int not null);



DECLARE
  --User-defined exception
  custom_exception EXCEPTION;
  
  --  I am associating the exception with Oracle error ORA-01400 in which we cannot insert NULL into a column
  PRAGMA EXCEPTION_INIT(custom_exception, -1400);
  
BEGIN
  -- I am attempting to insert a NULL value into a column sal that does not allow NULL
  INSERT INTO emp3 (empno,sal) VALUES (1,NULL);
  
EXCEPTION
  WHEN custom_exception THEN
    DBMS_OUTPUT.PUT_LINE('Custom error occurred: cannot insert NULL into a non-nullable column.');
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Other error occurred: ' || SQLERRM);
END;
/



Comments

Popular posts from this blog

Difference between union and union all in oracle sql plsql

"This is one of the most important questions that interviewer might be asking in an interview.Infyosis,Linked group,Kyndryl,Vodafone Idea,IBM and Mphasis etc" INTRODUCTION: It is said to be set operators which is used to combine, add or concatenate two or more result sets. It is used in the multiple select statements where to combine to produce the final desired results.In other words,multiple select statements of similar column name in same order and having same data type to produce the unified result.In MIS datalake,when you are adding the modules of GST reconciliation which is to be used in micros in Excel or VB script, union and union all set operator is preferred for eliminating duplicate records or keeping duplicate records as per business requirement. Column must be of the same datatype for these operations of one select statement with another select statements. Columns must be in the same order in this set operators in the multiple select statements. Same number of co...

Difference between decode and case oracle sql plsql interview question and answer

" This is the most accepted question for PLSQL developer role.It has been asked in many interviews like Mphasis and Kyndryl. "  INTRODUCTION: Case was came after oracle database version 8.1.6 whereas decode was came before oracle database version 8.1.6 .Decode and case statements in an oracle work or acts like if then elsif conditional statements. if sal='2000000' then 'General Manager' elsif sal='3000000' then 'Senior Manager' elsif sal='1200000' then 'Manager' else 'Employee' end if; create table student_details(Student_RollNo Number,Student_Name Varchar2(20),student_marks Number,student_result varchar2(1)); insert into student_details values (100,'Rahul',90,'P'); insert into student_details values (101,'Rakesh',30,'F'); insert into student_details values (102,'Ram',60,'P'); insert into student_details values (103,'Rohan',10,'F'); insert into student_detai...

Explain oracle pl sql datatypes interview question and answer for fresher and experienced.

 Oracle PLSQL Data Types : PLSQL constant,value,variable,parameter,function which return the value are having the data type. It will determine the storage space information occupied and occurred in the memory or storage format and range constraints for valid values and operations as per specified size limit. It will determine how the data will be going to stored, handled and treated by the oracle engine during the period of processing and storage. It is used in the PLSQL blocks. It allows you to define your sub-types. PLSQL codes are embedded into the java program. Subtypes are used to make the data types compatible with data types used in the PLSQL program while embedding the PLSQL code into the java program. Subtype will be compatible with ANSI/ISO & IBM. It provides the pre-defined data types. Pre-defined data types are categorized into the four types: Composite LOB Reference Scalar Scalar Datatypes: It stores the values without the internal components. It holds the single ...