RAISE_APPLICATION_ERROR can be used in either (or both) the executable section and the exception section of a PL/SQL program. The error number and message is displayed to the user.
Syntax:
RAISE_APPLICATION_ERROR ({error_number}, {error_message});
{error_number} - The Error number must be between -20000 and -20999
{error_message} - The Error message is the message you want to display when the error occurs
Using in Execution section
BEGIN
...
RAISE_APPLICATION_ERROR (-20001,'This is a user error message');
...
END;
Using in Exception section:
BEGIN
...
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR (-20003, 'This is another user error message');
END;
Examples:
Input:
DECLARE
l_number NUMBER;
BEGIN
SELECT 2 INTO l_number FROM DUAL;
dbms_output.put_line('l_number = '||l_number);
END;
Output:
l_number = 2
Input:
DECLARE
l_number NUMBER;
BEGIN
SELECT 2 INTO l_number FROM DUAL;
RAISE_APPLICATION_ERROR (-20001,'This is a user error message');
dbms_output.put_line('l_number = '||l_number);
END;
Output:
There is no output due to our ERROR
line 19: ORA-20001: This is a user error message
ORA-06512: at line 5
No comments:
Post a Comment