Deep analysis of raise anomaly in Oracle _oracle

Source: Internet
Author: User
Tags exception handling sql error throw exception
There are three different ways to throw an exception
1. Run-time engine via Pl/sql
2. Using the Raise statement
3. Call Raise_application_error Stored Procedure
When a database or Pl/sql error occurs at run time, an exception is thrown automatically by the Pl/sql runtime engine. Exceptions can also be thrown by raise statements
RAISE Exception_name;
An explicit throw exception is a idiom for programmers to handle declared exceptions, but raise is not limited to declared exceptions, and it can throw any exception. For example, if you want to detect a new Run-time exception handler with a Timeout_on_resource error, you simply use the following statement in your program:
RAISE Timeout_on_resouce;
For example, the following example of an order entry, if the order is less than the inventory quantity, then throw an exception, and catch the exception, handling exceptions
Copy Code code as follows:

DECLARE
Inventory_too_low EXCEPTION;
---Other declaration statements
BEGIN
IF Order_rec.qty>inventory_rec.qty THEN
RAISE Inventory_too_low;
End IF
EXCEPTION
When Inventory_too_low THEN
order_rec.staus:= ' backordered ';
End;

The Raise_application_error function is used to throw an exception and give the exception an error number and an error message. The default error number for the custom exception is +1, and the default information is user_defined_exception.  The Raise_application_error function can be invoked in the execution part of the PL/SQL program block and in the exception section to explicitly throw a named exception with a special error number. Raise_application_error (Error_number,message[,true,false])
The range of error numbers is-20,000 to-20,999. The error message is a text string, up to 2048 bytes. True and False indicates whether to add (true) to the error heap or overwrite (overwrite) error heap (false). By default, False.
As shown in the following code:
Copy Code code as follows:

IF Product_not_found THEN
Raise_application_error ( -20123, ' Invald product Code ', TRUE);
End IF;

--------------------------------------------------------------------------------------------------
When the exception is thrown, control unconditionally goes to the exception section, which means that control cannot return to the location where the exception occurred, and when the exception is processed and resolved, the control returns to the next statement in the previous layer's execution section.
Copy Code code as follows:

BEGIN
DECLARE
Bad_credit exception;
BEGIN
RAISE bad_credit;
--Exception occurred, control turn;
EXCEPTION
when Bad_credit THEN
dbms_output.put_line (' Bad_credit ');
End;
--bad_credit After exception handling, control is transferred here
EXCEPTION
When others THEN
-control does not go from Bad_credit exception to here
-because Bad_credit has been processed
End;
When an exception occurs, the control will go to or propagate to the exception-handling portion of the previous block when there is no such exception handler inside the block.
BEGIN
DECLARE---inner block start
Bad_credit exception;
BEGIN
RAISE bad_credit;
--Exception occurred, control turn;
EXCEPTION
When Zero_divide THEN--Cannot handle Bad_credite exception
Dbms_output.put_line (' DIVIDE by Zer O error ');
End--closing the inner block
--control cannot get here because the exception is not resolved;
--Exception part
EXCEPTION
When others THEN
--because Bad_credit is not resolved, control Will go to here
end;
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.