How to get the line number where an exception occurred in the stored procedure

I am working with a large stored procedure. Currently I have this exception code below to catch any exceptions which occur. What I really need information on, is what line number the exception occurred on.  Is there a way to get the line number where the error occurred?



EXCEPTION
 WHEN OTHERS THEN
  errMsg := "Error occurred ' || SUBSTR(SQLERRM, 1, 199);
  dbms_output.put_line(errMsg);
  RETURN;
LVL 2
brgdotnetcontractorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

slightwv (䄆 Netminder) Commented:
I don't know of a way to get the line number directly but take a look at DBMS_UTILITY.FORMAT_ERROR_BACKTRACE.

There is a good paper on it here:
http://www.oracle.com/technetwork/testcontent/o25plsql-093886.html
1
Mark GeerlingsDatabase AdministratorCommented:
That is the problem with having a generic "exception" section at the end of a large procedure: Oracle does not tell you then which line in the procedure the error actually happened on.  Your options are:
1. Remove or comment out the generic "exception" section at the end
2. Add a varchar2(2) variable in the declare section of the procedure, named something like: v_step; add lines like this in various places in the procedure: "v_step := '1';",  "v_step := '2';" etc;  add this command in your "exception" section (and make sure that you have "serveroutput on") : dbms_output_put_line('After step: '||v_step);
3. Add nested "begin ...exception ...end" sections around each query or SQL statement in the procedure, so the error message in each local exception block can be very specific.
1

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.