Sub topic 12
Capturing the Error Stack and Backtrace
Assume you know basic PL/SQL blocks, procedures, exception handlers, and transaction control. This chapter focuses on diagnosing an exception after it reaches a handler.
Three views of an error
PL/SQL provides several ways to inspect a failure:
SQLCODEreturns the numeric error code. For example, division by zero returns-1476.SQLERRMreturns the message for the current exception.DBMS_UTILITY.FORMAT_ERROR_STACKreturns the messages in the current error stack.DBMS_UTILITY.FORMAT_ERROR_BACKTRACEshows the source line where the exception was raised.DBMS_UTILITY.FORMAT_CALL_STACKshows the active PL/SQL call chain.
SQLCODE and SQLERRM describe the current error. The formatted stack and backtrace provide more context, especially when several procedures call one another.
Capturing diagnostics in a handler
The following block creates a nested procedure so the backtrace can show where the error began. DBMS_OUTPUT displays the diagnostic values.
SET SERVEROUTPUT ON;
DECLARE
PROCEDURE calculate_total IS
l_value NUMBER;
BEGIN
l_value := 10 / 0;
END calculate_total;
BEGIN
calculate_total;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('SQLCODE: ' || SQLCODE);
DBMS_OUTPUT.PUT_LINE('SQLERRM: ' || SQLERRM);
DBMS_OUTPUT.PUT_LINE('Error stack:');
DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_STACK);
DBMS_OUTPUT.PUT_LINE('Error backtrace:');
DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
DBMS_OUTPUT.PUT_LINE('Call stack:');
DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_CALL_STACK);
RAISE;
END;
/The exact line numbers depend on how you format the script. The backtrace should identify the line containing 10 / 0, even though the handler is in the outer block.
FORMAT_ERROR_BACKTRACE is especially useful in shared procedures. A handler may run in many contexts, but the backtrace identifies the original source location. FORMAT_CALL_STACK helps explain which procedures were active at that moment.
Preserving the original failure
Use RAISE without an argument to re-raise the exception currently being handled. This preserves its original code, message, and backtrace.
Avoid replacing the original exception with a vague message:
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
RAISE;
END;
/If you need to add application context, RAISE_APPLICATION_ERROR has a third parameter. Passing TRUE adds the new message to the existing error stack:
RAISE_APPLICATION_ERROR(
-20050,
'While processing payroll batch',
TRUE
);Use this carefully. A new application error can change what the caller sees, while a plain RAISE keeps the original exception intact.
> callout note > Record the error stack, backtrace, and relevant business identifiers before re-raising. Do not log only SQLERRM; the message may not identify the failing source line.
Common mistakes
- Do not use
WHEN OTHERSto silently ignore failures. - Do not call diagnostic functions only after the exception has been replaced.
- Do not assume the handler's line is the line that caused the failure.
- Do not expose internal stack details directly to end users. Store them in a protected log and return a suitable application message.
Exercise
Which function identifies the source line where an exception was originally raised, and which statement preserves the original exception after diagnostics are captured?
Show answer
DBMSUTILITY.FORMATERROR_BACKTRACE identifies the source line. RAISE without an argument re-raises the original exception.