The answers have mentioned both, $$PLSQL_LINE & DBMS_UTILITY.FORMAT_ERROR_BACKTRACE. But I would like to add a bit about the difference between them:
- Predefined Inquiry Directives
$$PLSQL_LINE & $$PLSQL_UNIT
PLSQL_LINE predefined inquiry directive is a PLS_INTEGER literal value indicating the line number reference to $$PLSQL_LINE in the current program unit.
From its definition, PLSQL_LINE is not suitable for exceptions logging because it will provide the line number of the exception, rather than the line number of the error occurred itself. This makes it difficult to detect the error location especially with big program units, unless you wrap every statement with exception handler as Jeffrey answer’s stated.
However, the good thing about PLSQL_LINE, it provides the number without the need of any extraction, or string parsing. Hence, it could be more suitable for other logging purposes. - DBMS_UTILITY.FORMAT_ERROR_BACKTRACE
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 advantage of using this procedure, is that it provides the exact line number in the program using where the error occurs, and not where the procedure call appears
However, the procedure returns a string likeORA-XXXXX: at "<program_unit_name>", line xx. So if you are interested in extracting the line number itself, for whatever logging purpose you want, you will need parse the string.
Finally, to make the difference clear, below are two procedures, with the same content. You can run them and notice the output difference
CREATE OR REPLACE PROCEDURE proc_plsql_line
IS
BEGIN
RAISE VALUE_ERROR;
EXCEPTION
WHEN VALUE_ERROR
THEN
DBMS_OUTPUT.put_line ( 'Error raised in: '|| $$plsql_unit ||' at line ' || $$plsql_line || ' - '||sqlerrm);
END;
/
And
CREATE OR REPLACE PROCEDURE proc_backtrace
IS
BEGIN
RAISE VALUE_ERROR;
EXCEPTION
WHEN VALUE_ERROR
THEN
DBMS_OUTPUT.put_line ( 'Error raised: '|| DBMS_UTILITY.FORMAT_ERROR_BACKTRACE || ' - '||sqlerrm);
END;
/
Execution:
exec proc_plsql_line;
Error raised in: PROC_PLSQL_LINE at line 8 - ORA-06502: PL/SQL: numeric or value error
exec proc_backtrace;
Error raised: ORA-06512: at "PROC_BACKTRACE", line 4 - ORA-06502: PL/SQL: numeric or value error