how to do exception handling in oracle ?

when I execute the below code I get some custom message from oracle saying Oracle 123415, data not found in the report.

How can I put my own custom message on that saying "You are not an authorized user...." with no number to it in the output.
 


PROCEDURE sp_get_data (
      p_std_id            IN       VARCHAR2,
      p_test_users   OUT      t_ref_test_users   )
   IS
      v_user_type   PLS_INTEGER;
   BEGIN
      v_user_type := f_get_user_type (p_std_id);
 
      -- implies not an SCM user , do not get any data back to the user
      IF v_user_type = 0  THEN
        RAISE NO_DATA_FOUND;
      END IF;
 
      OPEN p_test_users FOR
         END sp_get_data;

Open in new window

tech_questionAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

joebednarzCommented:
You could do this:

BEGIN
...
   IF v_user_type = 0 THEN
      RAISE NO_DATA_FOUND;
   END IF;
...

EXCEPTION WHEN NO_DATA_FOUND THEN
   OPEN p_test_users FOR SELECT 'You are not an authorized user...' FROM dual;
END;
0
Jinesh KamdarCommented:
Try something like this.

>> with no number to it in the output.
Not possible, all Oracle exceptions have to linked to an error code.
For user-defined exceptions, you can choose any number between -20000 and -20999.

Here's a ref :-


PROCEDURE sp_get_data (p_std_id     IN  VARCHAR2,
                       p_test_users OUT t_ref_test_users)
IS
v_user_type  PLS_INTEGER;
BEGIN
v_user_type := f_get_user_type(p_std_id);
 
-- implies not an SCM user , do not get any data back to the user
IF v_user_type = 0  THEN
   RAISE_APPLICATION_ERROR(-20002, 'You are not an autorized user...');
END IF;
 
OPEN p_test_users FOR (SELECT * FROM DUAL); ---> Your SELECT goes here.
END sp_get_data;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.