Oracle Bi Solutions

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Wednesday, 9 January 2013

Exception Handling

Posted on 10:30 by Unknown
Named System Exceptions

What is a named system exception?

Named system exceptions are exceptions that have been given names by PL/SQL. They are named in the STANDARD package in PL/SQL and do not need to be defined by the programmer.
Oracle has a standard set of exceptions already named as follows:
Oracle Exception NameOracle ErrorExplanation
DUP_VAL_ON_INDEXORA-00001You tried to execute an INSERT or UPDATE statement that has created a duplicate value in a field restricted by a unique index.
TIMEOUT_ON_RESOURCEORA-00051You were waiting for a resource and you timed out.
TRANSACTION_BACKED_OUTORA-00061The remote portion of a transaction has rolled back.
INVALID_CURSORORA-01001You tried to reference a cursor that does not yet exist. This may have happened because you've executed a FETCH cursor or CLOSE cursor before OPENing the cursor.
NOT_LOGGED_ONORA-01012You tried to execute a call to Oracle before logging in.
LOGIN_DENIEDORA-01017You tried to log into Oracle with an invalid username/password combination.
NO_DATA_FOUNDORA-01403You tried one of the following:
  1. You executed a SELECT INTO statement and no rows were returned.
  2. You referenced an uninitialized row in a table.
  3. You read past the end of file with the UTL_FILE package.
TOO_MANY_ROWSORA-01422You tried to execute a SELECT INTO statement and more than one row was returned.
ZERO_DIVIDEORA-01476You tried to divide a number by zero.
INVALID_NUMBERORA-01722You tried to execute an SQL statement that tried to convert a string to a number, but it was unsuccessful.
STORAGE_ERRORORA-06500You ran out of memory or memory was corrupted.
PROGRAM_ERRORORA-06501This is a generic "Contact Oracle support" message because an internal problem was encountered.
VALUE_ERRORORA-06502You tried to perform an operation and there was a error on a conversion, truncation, or invalid constraining of numeric or character data.
CURSOR_ALREADY_OPENORA-06511You tried to open a cursor that is already open.
The syntax for the Named System Exception in a procedure is:
CREATE [OR REPLACE] PROCEDURE procedure_name
[ (parameter [,parameter]) ]
IS
[declaration_section]

BEGIN
executable_section

EXCEPTION
WHEN exception_name1 THEN
[statements]

WHEN exception_name2 THEN
[statements]

WHEN exception_name_n THEN
[statements]

WHEN OTHERS THEN
[statements]

END [procedure_name];
The syntax for the Named System Exception in a function is:
CREATE [OR REPLACE] FUNCTION function_name
[ (parameter [,parameter]) ]
RETURN return_datatype
IS | AS
[declaration_section]

BEGIN
executable_section



EXCEPTION
WHEN exception_name1 THEN
[statements]

WHEN exception_name2 THEN
[statements]

WHEN exception_name_n THEN
[statements]

WHEN OTHERS THEN
[statements]

END [function_name];
Here is an example of a procedure that uses a Named System Exception:
CREATE OR REPLACE PROCEDURE add_new_supplier
(supplier_id_in IN NUMBER, supplier_name_in IN VARCHAR2)
IS

BEGIN
INSERT INTO suppliers (supplier_id, supplier_name )
VALUES ( supplier_id_in, supplier_name_in );

EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
raise_application_error (-20001,'You have tried to insert a duplicate supplier_id.');

WHEN OTHERS THEN
raise_application_error (-20002,'An error has occurred inserting a supplier.');

END;
In this example, we are trapping the Named System Exception called DUP_VAL_ON_INDEX. We are also using the WHEN OTHERS clause to trap all remaining exceptions.



Named Programmer-Defined Exceptions



What is a named programmer-defined exception?

Sometimes, it is necessary for programmers to name and trap their own exceptions - ones that aren't defined already by PL/SQL. These are called Named Programmer-Defined Exceptions.
The syntax for the Named Programmer-Defined Exception in a procedure is:
CREATE [OR REPLACE] PROCEDURE procedure_name
[ (parameter [,parameter]) ]
IS
[declaration_section]

exception_name EXCEPTION;

BEGIN
executable_section
RAISE exception_name;

EXCEPTION
WHEN exception_name THEN
[statements]

WHEN OTHERS THEN
[statements]

END [procedure_name];
The syntax for the Named Programmer-Defined Exception in a function is:
CREATE [OR REPLACE] FUNCTION function_name
[ (parameter [,parameter]) ]
RETURN return_datatype

IS | AS
[declaration_section]

exception_name EXCEPTION;

BEGIN
executable_section

RAISE exception_name;

EXCEPTION
WHEN exception_name THEN
[statements]

WHEN OTHERS THEN
[statements]

END [function_name];
Here is an example of a procedure that uses a Named Programmer-Defined Exception:
CREATE OR REPLACE PROCEDURE add_new_order
(order_id_in IN NUMBER, sales_in IN NUMBER)
IS
no_sales EXCEPTION;

BEGIN
IF sales_in = 0 THEN
RAISE no_sales;

ELSE
INSERT INTO orders (order_id, total_sales )
VALUES ( order_id_in, sales_in );
END IF;

EXCEPTION
WHEN no_sales THEN
raise_application_error (-20001,'You must have sales in order to submit the order.');

WHEN OTHERS THEN
raise_application_error (-20002,'An error has occurred inserting an order.');

END;
In this example, we have declared a Named Programmer-Defined Exception called no_sales in our declaration statement with the following code:
no_sales EXCEPTION;
We've then raised the exception in the executable section of the code:
IF sales_in = 0 THEN
RAISE no_sales;
Now if the sales_in variable contains a zero, our code will jump directly to the Named Programmer-Defined Exception called no_sales.
Finally, we tell our procedure what to do when the no_sales exception is encountered by including code in the WHEN clause:
WHEN no_sales THEN
raise_application_error (-20001,'You must have sales in order to submit the order.');
We are also using the WHEN OTHERS clause to trap all remaining exceptions:
WHEN OTHERS THEN
raise_application_error (-20002,'An error has occurred inserting an order.');

WHEN OTHERS Clause


What is a WHEN OTHERS clause?

The WHEN OTHERS clause is used to trap all remaining exceptions that have not been handled by your Named System Exceptions and Named Programmer-Defined Exceptions.
The syntax for the WHEN OTHERS clause in a procedure is:
CREATE [OR REPLACE] PROCEDURE procedure_name
[ (parameter [,parameter]) ]
IS
[declaration_section]

BEGIN
executable_section

EXCEPTION
WHEN exception_name1 THEN
[statements]

WHEN exception_name2 THEN
[statements]

WHEN exception_name_n THEN
[statements]

WHEN OTHERS THEN
[statements]

END [procedure_name];
The syntax for the WHEN OTHERS clause in a function is:
CREATE [OR REPLACE] FUNCTION function_name
[ (parameter [,parameter]) ]
RETURN return_datatype
IS | AS
[declaration_section]

BEGIN
executable_section

EXCEPTION
WHEN exception_name1 THEN
[statements]

WHEN exception_name2 THEN
[statements]

WHEN exception_name_n THEN
[statements]

WHEN OTHERS THEN
[statements]

END [function_name];
Here is an example of a procedure that uses a WHEN OTHERS clause:
CREATE OR REPLACE PROCEDURE add_new_order
(order_id_in IN NUMBER, sales_in IN NUMBER)
IS
no_sales EXCEPTION;

BEGIN
IF sales_in = 0 THEN
RAISE no_sales;

ELSE
INSERT INTO orders (order_id, total_sales )
VALUES ( order_id_in, sales_in );
END IF;

EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
raise_application_error (-20001,'You have tried to insert a duplicate order_id.');

WHEN no_sales THEN
raise_application_error (-20001,'You must have sales in order to submit the order.');

WHEN OTHERS THEN
raise_application_error (-20002,'An error has occurred inserting an order.');

END;
In this example, if an exception is encountered that is not a DUP_VAL_ON_INDEX or a no_sales, it will be trapped by the WHEN OTHERS clause.

Frequently Asked Questions


Question: Is there any way to get the ORA error number (and/or description) for the errors that will fall into OTHERS?
Something like:
WHEN OTHERS THEN
'Error number ' & Err.Number & ' has happened.'
Answer: Yes, you can use SQLCODE function to retrieve the error number and SQLERRM function to retrieve the error message.
For example, you could raise the error as follows:
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);

END;
Or you could log the error to a table as follows:
EXCEPTION
WHEN OTHERS THEN
err_code := SQLCODE;
err_msg := substr(SQLERRM, 1, 200);

INSERT INTO audit_table (error_number, error_message)
VALUES (err_code, err_msg);
END;




SQLCODE Function



What does the SQLCODE Function do?

The SQLCODE function returns the error number associated with the most recently raised error exception. This function should only be used within the Exception Handling section of your code:
EXCEPTION
WHEN exception_name1 THEN
[statements]

WHEN exception_name2 THEN
[statements]

WHEN exception_name_n THEN
[statements]

WHEN OTHERS THEN
[statements]

END [procedure_name];
You could use the SQLCODE function to raise an error as follows:
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
END;
Or you could log the error to a table as follows:
EXCEPTION
WHEN OTHERS THEN
err_code := SQLCODE;
err_msg := substr(SQLERRM, 1, 200);

INSERT INTO audit_table (error_number, error_message)
VALUES (err_code, err_msg);
END;
Learn more about the SQLERRM Function.


SQLERRM Function



What does the SQLERRM Function do?

The SQLERRM function returns the error message associated with the most recently raised error exception. This function should only be used within the Exception Handling section of your code:
EXCEPTION
WHEN exception_name1 THEN
[statements]

WHEN exception_name2 THEN
[statements]

WHEN exception_name_n THEN
[statements]

WHEN OTHERS THEN
[statements]

END [procedure_name];
You could use the SQLERRM function to raise an error as follows:
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
END;
Or you could log the error to a table as follows:
EXCEPTION
WHEN OTHERS THEN
err_code := SQLCODE;
err_msg := substr(SQLERRM, 1, 200);

INSERT INTO audit_table (error_number, error_message)
VALUES (err_code, err_msg);
END;
Learn more about the SQLCODE Function.



Oracle Error Messages



http://www.techonthenet.com/oracle/errors/index.php










Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in PL/SQL | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • [ODI] - Frequently Asked Questions (FAQ)
    Here is a list of FAQs about Oracle Data Integrator 1) What is Oracle Data Integrator (ODI)? 2) What is E-LT? 3) What components make up Ora...
  • Upper Function
    In Oracle/PLSQL, the  upper function  converts all letters in the specified string to uppercase. If there are characters in the string that ...
  • OBIEE 11g Hide/Show Sections based on Dashboard Prompt
    allow a user’s interaction to hide/show certain sections of a dashboard. In this particular case the user wanted to choose either ‘Quarterly...
  • OBIEE 11g not showing new dashboard in the drop down menu
    When creating New dashboard in  OBIEE 11g, I have faced with issue that dashboard name did not show up in drop down dashboard menu. 1. When ...
  • [OBIEE11g] - OBIEE Dashboard for Informatica Metadata Reporting
    The metadata that Informatica Power Center 8 retains in its repository can be exposed via OBIEE reports and dashboards. This metadata includ...
  • Data Modeling: Schema Generation Issue with ERwin Data Modeler 7.3
    We are using Computer Associate’s ERwin Data Modeler 7.3 for data modeling. In one of our engagements, we are pushing data model changes to ...
  • Informatica Powercenter Workflow Manager Repository Navigator docking float issue
    In case you’re also experiencing where your Repository Navigator is not dock or attached and it seems just floating within Workflow Manager ...
  • OBIEE 11g - State: HY000. Code: 10058. [NQODBC] [SQL_STATE: HY000] [nQSError: 10058] A general error has occurred. [nQSError: 46118] Out of disk space. (HY000)
    Error Details Error Codes: AAD5E5X3:OPR4ONWY:U9IM8TAC Odbc driver returned an error (SQLFetchScroll). State: HY000. Code: 10058. [NQODBC] [S...
  • To_Char Function
    In Oracle/PLSQL, the  to_char function  converts a number or date to a string. Syntax The syntax for the  to_char function  is: to_char( val...
  • OBIEE 11g - Query Limit
    Query limit and number of minutes a query can run per physical layer database connection, follow the below steps. > Login to Repository u...

Categories

  • BI Publisher
  • DAC
  • DataWarehouse
  • Hyperion
  • Informatica
  • OBIEE
  • ODI
  • Oracle Applications EBS 12.1.3
  • Oracle Database
  • PL/SQL
  • SQL
  • Unix/Linux

Blog Archive

  • ▼  2013 (500)
    • ►  November (8)
    • ►  October (1)
    • ►  July (4)
    • ►  June (9)
    • ►  May (15)
    • ►  April (24)
    • ►  March (43)
    • ►  February (73)
    • ▼  January (323)
      • Uninstalling Obiee 11g instance on a linux red hat
      • OBIEE 11g not showing new dashboard in the drop d...
      • OBIEE11g Installation
      • Starting OBIEE 11g Services on Linux
      • OBIEE11g Timestamp differencess
      • DAC11g Installation on Windows Server 2008R2.
      • BI Apps 7.9.6.4 Installation in widows server 2008R2
      • [OBIEE11g] - Eventually succeeded, but encountered...
      • [OBIEE11g] - Blue Screen Error While Login With Bi...
      • [OBIEE11g] - No Log Found Error
      • [OBIEE11g] - Stream Closed Error when Click on cor...
      • OBIA 7.9.6.4 RPD And Catalog Shared
      • [OBIEE11g] - Destination Path too Long error while...
      • [OBIEE11G] - Lookup table is a new feature in obie...
      • [OBIEE11g] - Create Veriable in OBIEE11g.
      • [OBIEE11g] - Configuring LDAP Server to provide OB...
      • [OBIEE11g] - Authentication Failure in OBIEE 11g
      • [OBIEE11g] - Bing Map Integration with OBIEE 11g
      • [OBIEE11g] - OBIEE Dashboard for Informatica Metad...
      • Informatica PowerCenter Upgrading from Version 8.6...
      • Data Modeling: Schema Generation Issue with ERwin ...
      • [OBIEE11g] - DAC Reporting in OBIEE11g
      • [OBIEE11g] - Publisher 11g – Performance Monitorin...
      • [OBIEE11g] - Auto Start OBIEE 11g using Windows Se...
      • [OBIEE11g] - Upgrade OBIEE 11.1.1.5 To Latest Vers...
      • OBIEE11g - User Right Click Interaction Control w...
      • [OBIEE11g] - Customizing Prompts ‘All Column Value...
      • [OBIEE11g] - Choosing the Right OBIEE Visualization
      • OBIEE11g - 11.1.1.6 New Features
      • [OBIEE11g] - Certification with Siebel Marketing f...
      • [OBIEE11g] - Creating a Stacked Bar Chart.
      • [BI EE11g] – Managing Host Name Changes
      • [DAC] - Multi Source Loads With OBIA
      • [Informatica] - ERROR CODES: [CNX_53021 ],[DOM_100...
      • [Informatica] - Informatica PowerCenter Repository...
      • [Informatica] - Processing UNICODE Characters in I...
      • [Linux] - Unix/Linix Commands
      • [DAC] - Full Load Vs Incremental Load
      • [Informatica] - Installation of Informatica 9.0.1 ...
      • [Informatica] - SF_34004- Service initialization ...
      • [Oracle Database] - Linux OS and Oracle database S...
      • [Oracle Database] - Installion Oracle database11g ...
      • [Informatica] - RR_4053 : Row error occurred while...
      • [OBIEE11g] - Change the placement of currency name
      • [OBIEE11g] - Exception Occuring During OBIEE 11.1....
      • What is Indexing in a Database
      • [OBIEE11g] - Setting up OBIEE11g Admin Tool for OD...
      • [OBIEE11g] - Getting Top-N Sales Reps Using the TO...
      • [OBIEE11g] - Getting Top-N Sales Reps Using Result...
      • [OBIEE11g] - Getting Top-N Sales Reps for Year and...
      • [OBIEE11g] - Analyzing Sales for “N Years Top-10 S...
      • [OBIEE11g] - Drill Down to Sub Reports Passing Mul...
      • [OBIEE11g[ - Configuring BI Scheduler for iBots on...
      • [OBIEE 11g] - How Application Roles, Groups and Us...
      • [OBIEE11g] - Setting up Access Permissions to Repo...
      • [OBIEE11g] - Fixing Weblogic and bi_server1 startu...
      • [OBIEE11g] - Deleting and Re-Creating Users in We...
      • [OBIEE 11g] - Backup and Restore of OBIEE Filesyst...
      • [OBIEE11g] - Creating Effective Bar Graphs
      • [OBIEE] - Useful SQL statements in Business Intell...
      • [OBIEE11g] - Creating Dashboard Traversing Throug...
      • [OBIEE11g] - Database Connection Failure while cr...
      • [DAC] - Admin password recovery
      • [Oracle 11g] - Oracle Database 11g installation on...
      • [OBIEE11g] - Variables in Oracle OBIEE 11g
      • [OBIEE11g] - Installing OBIEE 11g on Linux Fedora 17
      • [OBIEE11g] - Table view Date Column controlled by...
      • [OBIEE11g] - Adding Tooltips and conditional colo...
      • [OBIEE11g] - Show top-N Sales Persons in BI Publi...
      • [OBIEE11g] - Creating Scrolling Ticker Views
      • [OBIEE11g] - Authentication first with LDAP then ...
      • [OBIEE11g] - Relocation of OBIEE MetaData Reposit...
      • [OBIEE11g] - Hierarchical Roll-Up and Individual T...
      • [OBIEE11g] - Creation of Sales Reps Hierarchy wit...
      • [OBIEE11g] - Using external table to Filter BI Ans...
      • [OBIEE11g] - Configuring of RPD deployed on Linux...
      • [OBIEE11g] - Configuring an ODBC DSN for the Oracl...
      • [ODI] - Frequently Asked Questions (FAQ)
      • [OBIA] - Oracle BI Applications - Frequently Asked...
      • [OBIEE 11g] - Maps - Frequently Asked Questions (FAQ)
      • [OBIEE11g] - The 11g Features You Maybe Didn't Know!
      • [OBIEE11g] - New Features with OBIEE 11.1.1.6
      • [OBIEE11g] - Dashboard Prompt - "Prompt User"
      • [OBIEE11g] - [46153] The configuration file (O:\us...
      • [Informatica] - Multiple Chart of Accounts Configu...
      • [OBIEE11g] - Customizing Pivot Table Error
      • [OBIEE11g] - How to get Month Start Date and Month...
      • [OBIEE11g] - How to get Week Start Date and Week E...
      • [OBIEE11g] - How to rename My Dashboard
      • Table Organization in OBAW (Oracle Business Analyt...
      • [OBIEE11g] Uninstall OBIEE 11g
      • [OBIEE11g] - Command Line Merging in OBIEE 10g/11g
      • BI Publisher report is showing incorrect date(Show...
      • [OBIEE11g] - Connectivity issue from OBIEE (in Sol...
      • [OBIEE 11g] - Installation on Red Hat Linux
      • [OBIEE11g] - Different ToolTip for different rows ...
      • [OBIEE11g] - Integrating OBIEE 11g with EPM worksp...
      • [DAC] Fail to create indices during DAC execution ...
      • [DAC] Oracle DAC issue in 64 Bit Machine
      • [OBIEE11g] Connection Pool Select Button is Disabl...
Powered by Blogger.

About Me

Unknown
View my complete profile