Detailed explanation of RAISE exceptions in Oracle

Source: Internet
Author: User


In Oracle, RAISE exception details are thrown in three ways: www.2cto.com 1. use the PL/SQL Runtime Engine 2. use RAISE Statement 3. call the RAISE_APPLICATION_ERROR stored procedure. When an error occurs during database or PL/SQL running, an exception is automatically thrown by the PL/SQL runtime engine. An exception can also be thrown using the RAISE exception_name statement. explicitly throwing an exception is a common practice for programmers to handle declared exceptions. However, RAISE is not limited to declared exceptions and can throw any exceptions. For example, if you want to use TIMEOUT_ON_RESOURCE error to detect a new runtime exception processor, you only need to use the following statement in the program: RAISE TIMEOUT_ON_RESOUCE; for example, the following order Input example, IF the order quantity is smaller than the inventory quantity, an EXCEPTION is thrown and the EXCEPTION is caught to handle the exception declare inventory_too_low EXCEPTION; --- Other declaration statements begin if order_rec.qty> inventory_rec.qty then raise inventory_too; end if exception when inventory_too_low THEN order_rec.staus: = 'backordered'; END; RAISE_APPLICATION_ERROR built-in function is used to throw an EXCEPTION and assign an error number and information to the EXCEPTION. The default error code for custom exceptions is + 1, and the default information is User_Defined_Exception. The RAISE_APPLICATION_ERROR function can be called in the execution part and exception part of the pl/SQL program block, explicitly throwing a naming exception with a special error code. Raise_application_error (error_number, message [, true, false]) the error number ranges from-20,000 to-20,999. The error message is a text string, which consists of up to 2048 bytes. TRUE and FALSE indicate whether to add (TRUE) to the error stack or overwrite (FALSE ). The default value is FALSE. The following code shows: IF product_not_found THEN RAISE_APPLICATION_ERROR (-20123, 'invald product Code', TRUE); end if; when an exception is thrown, control the unconditional conversion to the exception section, this means that the control cannot return to the exception location. When the exception is handled and resolved, the control returns the next statement to the execution part of the previous layer. Begin declare bad_credit exception; begin raise bad_credit; -- EXCEPTION occurred, steering control; exception WHEN bad_credit THEN dbms_output.put_line ('bad _ credentials'); END; -- after bad_credit EXCEPTION is handled, control to here exception when others then -- control will not go from bad_credit EXCEPTION to here -- because bad_credit has been processed END; WHEN an EXCEPTION occurs, WHEN the EXCEPTION processor does not exist inside the block, the control will go to or spread to the exception handling section of the previous block. Begin declare --- internal block start bad_credit exception; begin raise bad_credit; -- EXCEPTION occurs, control steering; exception WHEN ZERO_DIVIDE THEN -- cannot handle bad_credite EXCEPTION dbms_output.put_line ('divide by zero error '); END -- END internal block -- the control cannot be reached here because the EXCEPTION is not resolved; -- EXCEPTION part exception when others then -- the control will go here as bad_credit is not resolved;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.