• <tfoot id='pE9pp'></tfoot>

        <bdo id='pE9pp'></bdo><ul id='pE9pp'></ul>
      <i id='pE9pp'><tr id='pE9pp'><dt id='pE9pp'><q id='pE9pp'><span id='pE9pp'><b id='pE9pp'><form id='pE9pp'><ins id='pE9pp'></ins><ul id='pE9pp'></ul><sub id='pE9pp'></sub></form><legend id='pE9pp'></legend><bdo id='pE9pp'><pre id='pE9pp'><center id='pE9pp'></center></pre></bdo></b><th id='pE9pp'></th></span></q></dt></tr></i><div id='pE9pp'><tfoot id='pE9pp'></tfoot><dl id='pE9pp'><fieldset id='pE9pp'></fieldset></dl></div>
        <legend id='pE9pp'><style id='pE9pp'><dir id='pE9pp'><q id='pE9pp'></q></dir></style></legend>

      1. <small id='pE9pp'></small><noframes id='pE9pp'>

        Oracle PL/SQL - 使用自定义 SQLERRM 引发用户定义的异常

        时间:2023-09-20

          <tbody id='r9yCX'></tbody>
        <i id='r9yCX'><tr id='r9yCX'><dt id='r9yCX'><q id='r9yCX'><span id='r9yCX'><b id='r9yCX'><form id='r9yCX'><ins id='r9yCX'></ins><ul id='r9yCX'></ul><sub id='r9yCX'></sub></form><legend id='r9yCX'></legend><bdo id='r9yCX'><pre id='r9yCX'><center id='r9yCX'></center></pre></bdo></b><th id='r9yCX'></th></span></q></dt></tr></i><div id='r9yCX'><tfoot id='r9yCX'></tfoot><dl id='r9yCX'><fieldset id='r9yCX'></fieldset></dl></div>
        <legend id='r9yCX'><style id='r9yCX'><dir id='r9yCX'><q id='r9yCX'></q></dir></style></legend>
            <bdo id='r9yCX'></bdo><ul id='r9yCX'></ul>
            • <tfoot id='r9yCX'></tfoot>

                • <small id='r9yCX'></small><noframes id='r9yCX'>

                • 本文介绍了Oracle PL/SQL - 使用自定义 SQLERRM 引发用户定义的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  是否可以创建用户定义的异常并能够更改 SQLERRM?

                  Is it possible to create user-defined exceptions and be able to change the SQLERRM?

                  例如:

                  DECLARE
                      ex_custom       EXCEPTION;
                  BEGIN
                      RAISE ex_custom;
                  EXCEPTION
                      WHEN ex_custom THEN
                          DBMS_OUTPUT.PUT_LINE(SQLERRM);
                  END;
                  /
                  

                  输出是用户定义的异常".是否可以更改该消息?

                  The output is "User-Defined Exception". Is it possible to change that message?

                  这里有更多细节.

                  我希望这能说明我正在努力做得更好.

                  I hope this one illustrates what I'm trying to do better.

                  DECLARE
                      l_table_status      VARCHAR2(8);
                      l_index_status      VARCHAR2(8);
                      l_table_name        VARCHAR2(30) := 'TEST';
                      l_index_name        VARCHAR2(30) := 'IDX_TEST';
                      ex_no_metadata      EXCEPTION;
                  BEGIN
                  
                      BEGIN
                          SELECT  STATUS
                          INTO    l_table_status
                          FROM    USER_TABLES
                          WHERE   TABLE_NAME      = l_table_name;
                      EXCEPTION
                          WHEN NO_DATA_FOUND THEN
                              -- raise exception here with message saying
                              -- "Table metadata does not exist."
                              RAISE ex_no_metadata;
                      END;
                  
                      BEGIN
                          SELECT  STATUS
                          INTO    l_index_status
                          FROM    USER_INDEXES
                          WHERE   INDEX_NAME      = l_index_name;
                      EXCEPTION
                          WHEN NO_DATA_FOUND THEN
                              -- raise exception here with message saying
                              -- "Index metadata does not exist."
                              RAISE ex_no_metadata;
                      END;
                  
                  EXCEPTION
                      WHEN ex_no_metadata THEN
                          DBMS_OUTPUT.PUT_LINE('Exception will be handled by handle_no_metadata_exception(SQLERRM) procedure here.');
                          DBMS_OUTPUT.PUT_LINE(SQLERRM);
                  END;
                  /
                  

                  实际上,有几十个这样的子块.我想知道是否有一种方法可以为每个子块引发单个用户定义的异常,但让它给出不同的消息,而不是为每个子块创建单独的用户定义的异常.

                  In reality, there are dozens of those sub-blocks. I'm wondering if there's a way to have a single user-defined exception for each of those sub-blocks to raise, but have it give a different message, instead of creating a separate user-defined exception for each sub-block.

                  在 .NET 中,这有点像有一个像这样的自定义异常:

                  In .NET, it would be sort of like having a custom exception like this:

                      public class ColorException : Exception
                      {
                          public ColorException(string message)
                              : base(message)
                          {
                          }
                      }
                  

                  然后,一个方法应该是这样的:

                  And then, a method would have something like this:

                          if (isRed)
                          {
                              throw new ColorException("Red is not allowed!");
                          }
                  
                          if (isBlack)
                          {
                              throw new ColorException("Black is not allowed!");
                          }
                  
                          if (isBlue)
                          {
                              throw new ColorException("Blue is not allowed!");
                          }
                  

                  推荐答案

                  是的.你只需要使用 RAISE_APPLICATION_ERROR 函数.如果您还想为异常命名,则需要使用 EXCEPTION_INIT 编译指示,以便将错误编号与命名异常相关联.类似的东西

                  Yes. You just have to use the RAISE_APPLICATION_ERROR function. If you also want to name your exception, you'll need to use the EXCEPTION_INIT pragma in order to associate the error number to the named exception. Something like

                  SQL> ed
                  Wrote file afiedt.buf
                  
                    1  declare
                    2    ex_custom EXCEPTION;
                    3    PRAGMA EXCEPTION_INIT( ex_custom, -20001 );
                    4  begin
                    5    raise_application_error( -20001, 'This is a custom error' );
                    6  exception
                    7    when ex_custom
                    8    then
                    9      dbms_output.put_line( sqlerrm );
                   10* end;
                  SQL> /
                  ORA-20001: This is a custom error
                  
                  PL/SQL procedure successfully completed.
                  

                  这篇关于Oracle PL/SQL - 使用自定义 SQLERRM 引发用户定义的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Oracle:是否有跟踪查询的工具,例如用于 sql server 的 Profiler? 下一篇:没有了

                  相关文章

                • <legend id='UFfxb'><style id='UFfxb'><dir id='UFfxb'><q id='UFfxb'></q></dir></style></legend>
                  • <bdo id='UFfxb'></bdo><ul id='UFfxb'></ul>

                    <i id='UFfxb'><tr id='UFfxb'><dt id='UFfxb'><q id='UFfxb'><span id='UFfxb'><b id='UFfxb'><form id='UFfxb'><ins id='UFfxb'></ins><ul id='UFfxb'></ul><sub id='UFfxb'></sub></form><legend id='UFfxb'></legend><bdo id='UFfxb'><pre id='UFfxb'><center id='UFfxb'></center></pre></bdo></b><th id='UFfxb'></th></span></q></dt></tr></i><div id='UFfxb'><tfoot id='UFfxb'></tfoot><dl id='UFfxb'><fieldset id='UFfxb'></fieldset></dl></div>

                    1. <tfoot id='UFfxb'></tfoot>

                      <small id='UFfxb'></small><noframes id='UFfxb'>