Thursday, August 29, 2013

ORCL - FORMAT_ERROR_BACKTRACE

Having a few packages to send email around, often I find exception error is hard to trace since I was just using SQLerrm function in exception, which only shows the error without telling the source.
1st I thought I could look for ways to find where exactly the error occurs, such as showing function/procedure name, then after a little online search, I found dbms_utility package has a nice function: FORMAT_ERROR_BACKTRACE, which displays the error line number, better than function/procedure name!
 
This procedure displays the call stack at the point where an exception was raised, even if the procedure is called from an exception handler in an outer scope. The output is similar to the output of the SQLERRM function, but not subject to the same size limitation.
Syntax
DBMS_UTILITY.FORMAT_ERROR_BACKTRACE 
  RETURN VARCHAR2;
Return value
 
NICE! Also if you want to look at full error stack, check FORMAT_ERROR_STACK function.
 
To use, just simply add such line (red) as the below:
 
EXCEPTION
    WHEN OTHERS THEN
      dbms_output.put_line (dbms_utility.FORMAT_ERROR_BACKTRACE);
      V_MSG_SUB_ERROR := V_MSG_SUB_ERROR;
      V_MSG_ERROR     := V_MSG_ERROR || SQLERRM || '
' || dbms_utility.FORMAT_ERROR_BACKTRACE || V_MSG_SIG;
      UTL_MAIL.SEND(SENDER     => EMAIL_FROM,
                    RECIPIENTS => EMAIL_ERROR_TO,
                    CC         => EMAIL_BCC,
                    SUBJECT    => V_MSG_SUB_ERROR,
                    MESSAGE    => V_MSG_ERROR,
                    MIME_TYPE  => 'text/html');
  END;

"In a real-world application, the error backtrace could be very long. Generally, debuggers and support people don't really want to have to deal with the entire stack; they are mostly going to be interested in that top-most entry. The developer of the application might even like to display that critical information to the users so that they can immediately and accurately report the problem to the support staff. In this case, it is necessary to parse the backtrace string and retrieve just the top-most entry." - by Steven Feuerstein 
 

No comments:

Post a Comment