title: Oracle PL/SQL - Raise User-Defined Exception With Custom SQLERRM
body: <p>Is it possible to create user-defined exceptions and be able to change the SQLERRM?</p>
  
  <p>For example:</p>
  
  <pre><code>DECLARE
      ex_custom       EXCEPTION;
  BEGIN
      RAISE ex_custom;
  EXCEPTION
      WHEN ex_custom THEN
          DBMS_OUTPUT.PUT_LINE(SQLERRM);
  END;
  /
  </code></pre>
  
  <p>The output is "User-Defined Exception".  Is it possible to change that message?</p>
  
  <p>EDIT:  Here is some more detail.</p>
  
  <p>I hope this one illustrates what I'm trying to do better.</p>
  
  <pre><code>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;
  /
  </code></pre>
  
  <p>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.</p>
  
  <p>In .NET, it would be sort of like having a custom exception like this:</p>
  
  <pre><code>    public class ColorException : Exception
      {
          public ColorException(string message)
              : base(message)
          {
          }
      }
  </code></pre>
  
  <p>And then, a method would have something like this:</p>
  
  <pre><code>        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!");
          }
  </code></pre>