Throws an exception in three different ways
1. Through the PL/SQL run-time engine
2. Using the Raise statement
3. Call the raise_application_error stored procedure
When a database or PL/SQL error occurs at run time, an exception is automatically thrown by the PL/SQL runtime engine. Exceptions can also be thrown by raise statements
RAISE Exception_name;
An explicit throw exception is a custom usage of the exception that the programmer handles the declaration, but raise is not limited to the exception that is declared, and it can throw any exception. For example, if you want to use the Timeout_on_resource error to detect a new runtime exception handler, you simply use the following statement in your program:
RAISE Timeout_on_resouce;
Example of an order entry below, if the order is less than the inventory quantity, throws an exception, catches the exception, handles the exception
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 built-in function is used to throw an exception and give the exception an error number along with an error message. The default error number for a custom exception is +1, and the default information is user_defined_exception. The Raise_application_error function can be called in the execution and exception portions of the PL/SQL program block, explicitly throwing 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 indicate whether to add (TRUE) the heap (error stack) or overwrite (overwrite) error heaps (FALSE). False by default.
As shown in the following code:
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 go back to where the exception occurred, and when the exception is processed and resolved, control returns to the next statement in the previous layer of execution.
BEGIN
DECLARE
Bad_credit exception;
BEGIN
RAISE Bad_credit;
--exception, control steering;
EXCEPTION
When Bad_credit and then
Dbms_output.put_line (' Bad_credit ');
END;
--bad_credit exception handling, control goes here
EXCEPTION
When OTHERS then
-control does not go from Bad_credit exception to here
-because Bad_ Credit has been processed by
END;
When an exception occurs where the exception handler is not inside the block, control is transferred to or propagated to the exception handling portion of the previous block.
BEGIN
DECLARE---inner block
Bad_credit exception;
BEGIN
RAISE Bad_credit;
-Exception occurred, control steering;
EXCEPTION
When zero_divide then-cannot handle Bad_credite exception
Dbms_output.put_line (' DIVIDE by ZERO Error ');
End--end inner block
-control cannot get here because the exception is not resolved;
--Exception section
EXCEPTION
When OTHERS and then
--because Bad_credit is not resolved, control will be transferred here
END;
Raise exception in Oracle