Raise Application Error Syntax
Contents |
to your PL/SQL program. With many programming languages, unless you disable error checking, a run-time error such raise_application_error vs raise as stack overflow or division by zero stops normal processing and
Raise_application_error(-20001
returns control to the operating system. With PL/SQL, a mechanism called exception handling lets you oracle raise bulletproof your program so that it can continue operating in the presence of errors. This chapter contains these topics: Overview of PL/SQL Runtime Error Handling Advantages raise application error in oracle triggers 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 Compile-Time Warnings Overview of PL/SQL Runtime Error Handling In PL/SQL, an error condition is called an exception. Exceptions can be
Raise_application_error Parameters
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 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 exec
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 Learn more about Stack Overflow the company http://stackoverflow.com/questions/1760596/oracle-what-is-the-situation-to-use-raise-application-error Business Learn more about hiring developers or posting ads with us Stack Overflow Questions http://www.toadworld.com/platforms/oracle/b/weblog/archive/2010/07/14/raise-vs-raise-application-error Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack 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_ERROR? up vote 13 down vote favorite 5 We can use RAISE to fire raise application 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 23 down vote accepted There are two uses for RAISE_APPLICATION_ERROR. The first is to replace generic Oracle exception messages with our raise application error 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 when dup_val_on_index then raise_application_error (-20001 , 'NEW_EMP::employee called '||p_name||' already exists' , true); when invalid_manager then raise_application_error (-20002 , 'NEW_EMP::'||p_mgr ||' is not a valid manager'); end; / How it looks: SQL> exec new_emp ('DUGGAN', 2500, 'SALES', 10, 7782, sysdate+1) BEGIN new_em
SQL Server PRODUCTSDiscussion & Resources Benchmark Factory Code Tester for Oracle SharePlex SQL Navigator SQL Optimizer Spotlight Stat Toad Intelligence Central Toad Data Modeler Toad Data Point Toad Extension for Eclipse Toad for Hadoop Toad for IBM DB2 Toad for Oracle Toad for MySQL Toad for SQL Server Toad for SAP Solutions Toad MAC Edition Home » Platforms » Oracle » Oracle Blog » RAISE vs RAISE_APPLICATION_ERROR? RAISE vs RAISE_APPLICATION_ERROR? Oracle Community Join Overview Forum Blog Wiki Members Blog Options Print Comment RSS Feed Tweet Related Posts "Our" Error Codes : -20999 to -20000 by Steven Feuerstein on 3 Feb 2012 0 comments RAISE_APPLICATION_ERROR latest revision over 3 years ago by Steve Hilker Who needs comments? My code is self-documenting! AKA: Comment tersely with value-added information. by Steven Feuerstein on 28 Nov 2012 0 comments View More RAISE vs RAISE_APPLICATION_ERROR? Follow Steven Feuerstein / 7.14.2010 at 9:01pm A PL/SQL developer just sent me the following question: "I can't understand the difference between RAISE_APPLICATION_ERROR and RAISE. Why would I use one vs. the other?" Instead of answering via an email to just one developer, I thought I might share my answer with everyone. The RAISE_APPLICATION_ERROR built-in (defined in the DBMS_STANDARD package) should be used for just a single scenario: you need to communicate an application-specific error back to the user. Suppose, for example, I have a rule for the employees table that the minimum salary allowed is $100,000 (ah, wouldn't that be nice?). I want to enforce that rule through a database trigger: TRIGGER employees_minsal_tr BEFORE INSERT OR UPDATE ON employees FOR EACH ROWBEGIN IF :new.salary < 100000 THEN /* communicate error */ NULL; END IF;END; I can stop the DML from completing by issuing a RAISE statement, such as: RAISE PROGRAM_ERROR; But I would not be able to communicate back to the user what the actual problem was. If, on the other hand, I use RAISE_APPLICATION_ERROR, I can specify the error number (of little interest to my users, but a good "identifier" for support) and, more importantly, the error message, as in: TRIGGER employees_minsal_tr BEFORE INSERT OR UPDATE ON employees FOR EACH ROWBEGIN IF :new.salary < 1000000 THEN RAISE_APPLICATION_ERROR (-20000, '