Suppose I have a PL/SQL function that selects one value from a table. If the query returns no records, I wish for the NO_DATA_FOUND
error to propagate (so that the calling code can catch it), but with a more meaningful error message when SQLERRM
is called.
Here is an example of what I am trying to accomplish:
FUNCTION fetch_customer_id(customer_name VARCHAR2) RETURN NUMBER;
customer_id NUMBER;
BEGIN
SELECT customer_id
INTO customer_id
FROM CUSTOMERS
WHERE customer_name = fetch_customer_id.customer_name;
RETURN customer_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
meaningful_error_message := 'Customer named ' || customer_name || ' does not exist';
RAISE;
END;
Is there a way to associate meaningful_error_message
with the NO_DATA_FOUND
error?
Update: It has been suggested that I use RAISE_APPLICATION_ERROR
to raise a custom error code when NO_DATA_FOUND
is encountered. The purpose of this question was to determine if this technique could be avoided so that the calling code can catch NO_DATA_FOUND
errors rather than a custom error code. Catching NO_DATA_FOUND
seems more semantically correct, but I could be wrong.