且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

发生错误时如何在PL/SQL中查找行号,过程名称

更新时间:2022-11-13 12:38:27

经过大量研究(头部撞牙和咬牙切齿),我遇到了这种模式:

I've come across this pattern after much research, head banging and gnashing of teeth:

CREATE OR REPLACE PACKAGE BODY my_schema.package_name
IS

  PROCEDURE foo
  IS
  BEGIN
    -- Call stored procedures/functions that throw unhandled exceptions
  EXCEPTION
    WHEN OTHERS THEN
      DBMS_OUTPUT.PUT_LINE('ERROR! - '
        || DBMS_UTILITY.FORMAT_ERROR_STACK
        || DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
  END foo;

END;

DBMS_UTILITY.FORMAT_ERROR_STACK函数似乎提供了错误代码和消息,而DBMS_UTILITY.FORMAT_ERROR_BACKTRACE似乎提供了诚实的善良堆栈跟踪,至少在Oracle 10g中具有行号和存储过程名称.

The DBMS_UTILITY.FORMAT_ERROR_STACK function seems to give the error code and message, and DBMS_UTILITY.FORMAT_ERROR_BACKTRACE seems to give an honest to goodness stack trace, complete with line numbers and stored procedure names in Oracle 10g at least.

我不确定这些功能在Oracle 9i中是否可用.即使对于Oracle 10g,我也找不到太多有关此类事情的信息,所以我认为我至少应该发布此答案,因为9i相当老(因此10g就可以了).

I'm not sure if those functions are available in Oracle 9i. I couldn't find much information about this sort of thing even for Oracle 10g, so I thought I would at least post this answer since 9i is quite old (and so it 10g for that matter).