I'm having an issue where the original stack trace gets lost when I catch an exception, then raise it.
- Exception gets thrown in proc_a
- Catch the exception.
- Perform a rollback.
- RAISE the exception.
- Catch the exception (parent block)
- Print Stack Trace:
SUBSTR(SQLERRM || chr(10) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE(), 1, 3999)
Example:
DECLARE
BEGIN
DECLARE
lv_val VARCHAR2(1);
BEGIN
SELECT dummy INTO lv_val -- Line# 6 (desired)
FROM dual
WHERE dummy = 'FFF';
EXCEPTION
WHEN OTHERS THEN
--DBMS_OUTPUT.PUT_LINE(SUBSTR(SQLERRM || chr(10) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE(), 1, 3999));
RAISE; -- Line# 12 (actual)
END;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SUBSTR(SQLERRM || chr(10) || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE(), 1, 3999));
END;
/
Desired Result:
The line number for the original exception (step 1).
ORA-01403: no data found
ORA-06512: at line 6
or
ORA-01403: no data found
ORA-06512: at line 12
Caused By:
ORA-01403: no data found
ORA-06512: at line 6
Actual Result:
The line number for the RAISE (Step 4).
ORA-01403: no data found
ORA-06512: at line 12
Additional attempts that failed:
SQLERRM || chr(10) || DBMS_UTILITY.FORMAT_ERROR_STACK()
ORA-01403: no data found
ORA-01403: no data found
SQLERRM || chr(10) || DBMS_UTILITY.FORMAT_CALL_STACK()
ORA-01403: no data found
----- PL/SQL Call Stack -----
object line object
handle number name
0xee1cbd68 18 anonymous block
DBMS_UTILITY.FORMAT_ERROR_BACKTRACE()
. AndDBMS_UTILITY.FORMAT_ERROR_STACK()
only shows the message which is the same for both since it was caught and then raised. AndDBMS_UTILITY.FORMAT_CALL_STACK
gives the wrong line number, too. – DiaphragmWHEN OTHERS
and do the rollback and whatever you need in the external exception handling part? – CaecilianDBMS_UTILITY.FORMAT_ERROR_STACK
andDBMS_UTILITY.FORMAT_ERROR_BACKTRACE
, that would be perfect since it would contain both stack traces. – Diaphragmdbms_utility.format_error_backtrace
already containssqlerrm
before concatenating them. However this is an old problem (and I suspect not unique to PL/SQL - I've hit the same situation in Perl for example). For some notes about the quantum mechanics of exception handling, see mikesmithers.wordpress.com/2015/07/24/…. – Epitaph