Raise Application Error Code Range
Contents |
to your PL/SQL program. With many programming languages, unless you disable error checking, a run-time error such as stack overflow or division by zero stops normal processing and returns control to the operating
Raise_application_error Vs Raise
system. With PL/SQL, a mechanism called exception handling lets you bulletproof your program so raise_application_error(-20001 that it can continue operating in the presence of errors. This chapter contains these topics: Overview of PL/SQL Runtime Error Handling
Oracle Raise
Advantages of PL/SQL Exceptions Summary of Predefined PL/SQL Exceptions Defining Your Own PL/SQL Exceptions How PL/SQL Exceptions Are Raised How PL/SQL Exceptions Propagate Reraising a PL/SQL Exception Handling Raised PL/SQL Exceptions Overview of PL/SQL raise_application_error parameters Compile-Time Warnings Overview of PL/SQL Runtime Error Handling In PL/SQL, an error condition is called an exception. Exceptions can be internally defined (by the runtime system) or user defined. Examples of internally defined exceptions include division by zero and out of memory. Some common internal exceptions have predefined names, such as ZERO_DIVIDE and STORAGE_ERROR. The other internal exceptions can be given names. You can define exceptions of your raise application error in oracle triggers own in the declarative part of any PL/SQL block, subprogram, or package. For example, you might define an exception named insufficient_funds to flag overdrawn bank accounts. Unlike internal exceptions, user-defined exceptions must be given names. When an error occurs, an exception is raised. That is, normal execution stops and control transfers to the exception-handling part of your PL/SQL block or subprogram. Internal exceptions are raised implicitly (automatically) by the run-time system. User-defined exceptions must be raised explicitly by RAISE statements, which can also raise predefined exceptions. To handle raised exceptions, you write separate routines called exception handlers. After an exception handler runs, the current block stops executing and the enclosing block resumes with the next statement. If there is no enclosing block, control returns to the host environment. For information on managing errors when using BULK COLLECT, see "Handling FORALL Exceptions with the %BULK_EXCEPTIONS Attribute". Example 10-1 calculates a price-to-earnings ratio for a company. If the company has zero earnings, the division operation raises the predefined exception ZERO_DIVIDE, the execution of the block is interrupted, and control is transferred to the exception handlers. The optional OTHERS handler catches all exceptions that the block does not name specifically. Example 10-1 Runtime Error Handling DECLARE stock_
FunctionsRegular Expressions FunctionsStatistical FunctionsLinear Regression FunctionsPL SQL Data TypesPL SQL StatementsPL SQL OperatorsPL SQL ProgrammingCursorCollectionsFunction Procedure PackagesTriggerSQL PLUS Session EnvironmentSystem Tables Data DictionarySystem PackagesObject OrientedXMLLarge ObjectsTransactionUser PrivilegeA
Raise_application_error Sqlerrm
complete example using RAISE_APPLICATION_ERROR : raise_application_error«PL SQL Programming«Oracle PL/SQL raise_application_error error number range TutorialOracle PL/SQL TutorialPL SQL Programmingraise_application_errorSQL> SQL> SQL> create table company( 2 product_id number(4)
Raise_application_error Number Range
not null, 3 company_id NUMBER(8) not null, 4 company_short_name varchar2(30) not null, 5 company_long_name varchar2(60) 6 ); Table created. SQL> insert into https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/errors.htm company values(1,1001,'A Inc.','Long Name A Inc.'); 1 row created. SQL> insert into company values(1,1002,'B Inc.','Long Name B Inc.'); 1 row created. SQL> insert into company values(1,1003,'C Inc.','Long Name C Inc.'); 1 row created. SQL> insert into company values(2,1004,'D Inc.','Long Name D Inc.'); 1 row created. http://www.java2s.com/Tutorial/Oracle/0480__PL-SQL-Programming/AcompleteexampleusingRAISEAPPLICATIONERROR.htm SQL> insert into company values(2,1005,'E Inc.','Long Name E Inc.'); 1 row created. SQL> insert into company values(2,1006,'F Inc.','Long Name F Inc.'); 1 row created. SQL> SQL> SQL> SQL> SQL> CREATE OR REPLACE PROCEDURE org_proc 2 (p_flag_in VARCHAR2, 3 p_product_id NUMBER, 4 p_company_id NUMBER, 5 p_company_short_name VARCHAR2, 6 p_company_long_name VARCHAR2) 7 IS 8 v_error_code NUMBER; 9 BEGIN 10 IF (p_flag_in ='I')THEN 11 BEGIN 12 INSERT INTO company VALUES(p_product_id,p_company_id,p_company_short_name,p_company_long_name); 13 EXCEPTION WHEN OTHERS THEN 14 v_error_code :=SQLCODE; 15 IF v_error_code =-1 THEN 16 RAISE_APPLICATION_ERROR(-20000,'Organization '||TO_CHAR(p_company_id)||' already exists.'); 17 ELSIF v_error_code =-2291 THEN 18 RAISE_APPLICATION_ERROR(-20001,'Invalid Hierarchy Code '||TO_CHAR(p_product_id)||' specified.'); 19 END IF; 20 END; 21 ELSIF (p_flag_in ='C')THEN 22 BEGIN 23 UPDATE company 24 set company_short_name =p_company_short_name, 25 company_long_name =p_company_long_name 26 WHERE product_id =p_product_id 27 AND company_id =p_company_id; 28 IF SQL%NOTFOUND THEN 29 RAISE_APPLICATION_ERRO
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us http://stackoverflow.com/questions/1760596/oracle-what-is-the-situation-to-use-raise-application-error Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack http://www.oraclenerd.com/2010/10/never-use-raiseapplicationerror-again.html Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Oracle: what is the situation to use raise application RAISE_APPLICATION_ERROR? up vote 13 down vote favorite 5 We can use RAISE to fire an exception. What particular situations do we need to use RAISE_APPLICATION_ERROR? Thanks. oracle share|improve this question asked Nov 19 '09 at 2:56 Ricky 8,3982762100 I found a useful link for similar question. toadworld.com/platforms/oracle/b/weblog/archive/2010/07/14/… –gmail user Feb 6 '14 at 16:30 add a comment| 4 Answers 4 active oldest votes up vote raise application error 23 down vote accepted There are two uses for RAISE_APPLICATION_ERROR. The first is to replace generic Oracle exception messages with our own, more meaningful messages. The second is to create exception conditions of our own, when Oracle would not throw them. The following procedure illustrates both usages. It enforces a business rule that new employees cannot be hired in the future. It also overrides two Oracle exceptions. One is DUP_VAL_ON_INDEX, which is thrown by a unique key on EMP(ENAME). The other is a a user-defined exception thrown when the foreign key between EMP(MGR) and EMP(EMPNO) is violated (because a manager must be an existing employee). create or replace procedure new_emp ( p_name in emp.ename%type , p_sal in emp.sal%type , p_job in emp.job%type , p_dept in emp.deptno%type , p_mgr in emp.mgr%type , p_hired in emp.hiredate%type := sysdate ) is invalid_manager exception; PRAGMA EXCEPTION_INIT(invalid_manager, -2291); dummy varchar2(1); begin -- check hiredate is valid if trunc(p_hired) > trunc(sysdate) then raise_application_error (-20000 , 'NEW_EMP::hiredate cannot be in the future'); end if; insert into emp ( ename , sal , job , deptno , mgr , hiredate ) values ( p_name , p_sal , p_job , p_dept , p_mgr , trunc(p_hired) ); exception whe
is. It is an abomination of hard-coding and poor practice. If you didn't know that, I'm sorry I was the one who told you. I've written and used extensively an ultra-simple framework to eliminate RAISE_APPLICATION_ERROR from my code forever.Here's an example (assume 11gR2) of something we all know we can do:begin dbms_output.put_line(1/0); end;This will throw an unhandled ORA-01476 exception. We could write some meaningful handling of that with this:begin dbms_output.put_line(1/0);exception when zero_divide then dbms_output.put_line('zero divide exception caught');end;This coding is elegant because Oracle has conveniently predefined an exception named ZERO_DIVIDE and a corresponding pragma for us. Unfortunately, Oracle has only 22 predefined exceptions. What happens when I do this:declare d date;begin d := to_date('2010-09-30', 'YYYY-MM-DD'); -- works dbms_output.put_line(d); d := to_date('12345-09-30', 'YYYY-MM-DD'); -- fails dbms_output.put_line(d);end;This will throw an unhandled ORA-01861 exception. My option to handle this is less than meaningful because this is not a predefined exception:declare d date;begin d := to_date('2010-09-30', 'YYYY-MM-DD'); -- works dbms_output.put_line(d); d := to_date('12345-09-30', 'YYYY-MM-DD'); -- fails dbms_output.put_line(d);exception when others then case sqlcode when -1861 then dbms_output.put_line('literal does not match exception caught'); else raise; end case;end;This leads me to the inevitable desire to create my own named exception and pragma, so I could have code that looks like this instead:declare d date;begin d := to_date('2010-09-30', 'YYYY-MM-DD'); -- works dbms_output.put_line(d); d := to_date('12345-09-30', 'YYYY-MM-DD'); -- fails dbms_output.put_line(d);exception when error.ora_literal_string_mismatch the