Plsql Catch Raise Application Error
Contents |
to your PL/SQL program. With many programming languages, unless you disable error checking, a run-time raise application error oracle example error such as stack overflow or division by zero stops
Oracle Raise
normal processing and returns control to the operating system. With PL/SQL, a mechanism called exception
Raise_application_error Vs Raise
handling lets you bulletproof your program so that it can continue operating in the presence of errors. This chapter contains these topics: Overview of PL/SQL
Raise_application_error(-20001
Runtime Error Handling 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 Compile-Time Warnings Overview of PL/SQL Runtime Error Handling In PL/SQL, an error condition is pl sql exception handling examples 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 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 call
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 complete example using RAISE_APPLICATION_ERROR : raise_application_error«PL SQL Programming«Oracle PL/SQL raise_application_error parameters TutorialOracle PL/SQL TutorialPL SQL Programmingraise_application_errorSQL> SQL> SQL> create table company( 2 pl sql continue after exception product_id number(4) not null, 3 company_id NUMBER(8) not null, 4 company_short_name varchar2(30) not null, 5 company_long_name raise application error in oracle triggers varchar2(60) 6 ); Table created. SQL> insert into 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.'); https://docs.oracle.com/cd/B19306_01/appdev.102/b14261/errors.htm 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. 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> http://www.java2s.com/Tutorial/Oracle/0480__PL-SQL-Programming/AcompleteexampleusingRAISEAPPLICATIONERROR.htm 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_ERROR(-20002,'Organization '||TO_CHAR(p_company_id)||' does not exist.'); 30 END IF; 31 END; 32 ELSIF (p_flag_in ='D')THEN 33 BEGIN 34 DELETE company 35 WHERE product_id =p_product_id 36 AND company_id =p_company_id; 37 IF SQL%NOTFOUND THEN 38 RAISE_APPLICATION_ERROR(-20003,'Organization '||TO_CHAR(p_company_id)||' does not exist.'); 39 END IF; 40 EXCEPTION WHEN OTHERS THEN 41 v_error_code :=SQLCODE; 42 IF v_error_code =-2292 THEN 43 RAISE_APPLICATION_ERROR(
SQL TuningSecurityOracle UNIXOracle LinuxMonitoringRemote supportRemote plansRemote servicesApplication Server ApplicationsOracle FormsOracle PortalApp UpgradesSQL ServerOracle ConceptsSoftware SupportRemote Support Development Implementation Consulting StaffConsulting PricesHelp Wanted! Oracle PostersOracle Books Oracle Scripts Ion Excel-DB Don Burleson Blog
http://www.dba-oracle.com/t_raise_application_error.htm RAISE_APPLICATION_ERROR tips Oracle Database Tips by Burleson http://www.codeproject.com/Questions/179091/How-to-catch-RAISE-APPLICATION-ERROR Consulting March 3, 2015 Question: What does the PL/SQL raise_application_error code do? Answer: The raise_application_error is actually a procedure defined by Oracle that allows the developer to raise an exception and associate an error number and message with the procedure. This allows the application to raise application errors rather than just Oracle errors. Error numbers raise application are defined between -20,000 and -20,999. Oracle provides the raise_application_error procedure to allow you to raise custom error numbers within your applications. You can generate errors and their associated text starting with -20000 and proceeding through -20999 (a grand total of 1,000 error numbers that you can use). Below we illustrate the use of the raise_application_error procedure. Using the raise_application_error procedure: DECLARE Balance integer := 24; BEGIN IF (nBalance raise application error <= 100) THEN Raise_Application_Error (-20343, 'The balance is too low.');END IF;END; In this example, error number -20343 is raised if the value of nBalance isn't greater than 100, yielding a message that looks like this: ORA-20343: The balance is too low. All other numbers belong to Oracle for its own errors. The message can be anything that will fit in a varchar2(2000). The final parameter passed to the procedure is a Boolean(true/false) that tells the procedure to add this error to the error stack or replace all errors in the stack with this error. Passing the value of 'True' adds the error to the current stack, while the default is 'False'. SQL> create or replace procedure test_var2 (n_test IN number := 0,3 n_result OUT number)4 as5 begin 6 if n_test > 100 then7 raise_application_error(-20010,'Number Too Large');8 end if;9 n_result := n_test;10 end; 11 / Procedure created. SQL> declare2 n_numb number := &Number;3 n_2 number := 0;4 begin5 test_var(n_numb, n_2);6 dbms_output.put_line(n_2);7 end;8 /Enter value for number: 5old 2: n_numb number := &Number;new 2: n_numb number := 5;5 PL/SQL procedure successfully completed. SQL> /Enter value for number: 105old 2: n_numb number := &Number;new 2: n_numb number := 105; declare*ERROR at line 1:ORA-20010: Number Too LargeORA-06512: at "Tips/Tricks Top Articles Beginner Articles Technical Blogs Posting/Update Guidelines Article Help Forum Article Competition Submit an article or tip Post your Blog quick answersQ&A Ask a Question View Unanswered Questions View All Questions... Linux questions C# questions ASP.NET questions SQL questions fabric questions discussionsforums All Message Boards... Application Lifecycle> Running a Business Sales / Marketing Collaboration / Beta Testing Work Issues Design and Architecture ASP.NET JavaScript C / C++ / MFC> ATL / WTL / STL Managed C++/CLI C# Free Tools Objective-C and Swift Database Hardware & Devices> System Admin Hosting and Servers Java .NET Framework Android iOS Mobile SharePoint Silverlight / WPF Visual Basic Web Development Site Bugs / Suggestions Spam and Abuse Watch features Competitions News The Insider Newsletter The Daily Build Newsletter Newsletter archive Surveys Product Showcase Research Library CodeProject Stuff communitylounge Who's Who Most Valuable Professionals The Lounge The Insider News The Weird & The Wonderful The Soapbox Press Releases Non-English Language > General Indian Topics General Chinese Topics help What is 'CodeProject'? General FAQ Ask a Question Bugs and Suggestions Article Help Forum Site Map Advertise with us About our Advertising Employment Opportunities About Us Ask a Question All Questions All Unanswered FAQ How to catch RAISE_APPLICATION_ERROR Rate this: Please Sign up or sign in to vote. See more: Oracle Hi, In Oracle, How do i catch the RAISE_APPLICATION_ERROR in Exception block? Like exception raised from RAISE_APPLICATION_ERROR always caught in 'WHEN OTHERS THEN' block? Please answer? Thanks Posted 8-Apr-11 9:09am web works715 Updated 12-Apr-11 6:51am v2 Add a Solution 2 solutions Top Rated Most Recent Rate this: Please Sign up or sign in to vote. Solution 1 Accept Solution Reject Solution Sounds like you are looking for this: Handling PL/SQL Errors[^] Overview of PL/SQL Error Handling[^] Permalink Posted 8-Apr-11 9:12am Sandeep Mewara505.5K Comments Mika Wendelius 8-Apr-11 15:27pm Essential links, 5'd Rate this: Please Sign up or sign in to vote. Solution 2 Accept Solution Reject Solution The easiest way is to predefine an error number (or numbers) which you would use in the RAISE_APPLICATION_ERROR call. In the EXCEPTION block you can now check if the error number is one of the predefined errors and do proper action. If it's not let it to fall to WHEN OTHERS section. The error numbers could be predefined for example in a package header. Permalink Posted 8-Apr-11 9:21am Mika Wendelius325.1K Add a