Search notes:

Oracle: RAISE_APPLICATION_ERROR

raise_application_error raises an exception.
raise_application_error(nnn, "message");
raise_application_error(nnn, "message", true);
raise_application_error(nnn, "message", false);
nnn: is the error code and must(?) be in the range -20000 through -20999.
If the third parameter of raise_application_error is true, the error code is put on top of the error stack, if it is false, the error stack is replaced with the error message.
The default for this parameter is false.
declare

   d number;

   procedure raise_error (b in boolean) is -- {
   begin

       raise_application_error(-20801, 'Some text', b);

   end raise_error; -- }

   function divide_by_zero(b in boolean) return number is -- {
   begin
       return 5/0;
   exception when others then
       raise_error(b);
   end divide_by_zero; -- }

   procedure print_error_stack is -- {
   begin
       dbms_output.put_line('error depth: ' || utl_call_stack.error_depth);
       dbms_output.put_line('  sqlerrm: ' || sqlerrm);
       dbms_output.put_line('');
       for d in 1 .. utl_call_stack.error_depth loop
           dbms_output.put_line('  ' || utl_call_stack.error_number(d) || ': ' || utl_call_stack.error_msg(d));
       end loop;

   end print_error_stack; -- }

begin

   begin
      d := divide_by_zero(false);
   exception when others then
      print_error_stack;
   end;

   dbms_output.put_line('----------------------');

   begin
      d := divide_by_zero(true);
   exception when others then
      print_error_stack;
   end;
end;
/
Github repository Oracle-Patterns, path: /PL-SQL/exception/raise_application_error.plsql
The previous example prints
error depth: 3
  sqlerrm: ORA-20801: Some text

  20801: Some text
  6512: at line 8
  6512: at line 16
----------------------
error depth: 5
  sqlerrm: ORA-20801: Some text

  20801: Some text
  6512: at line 8
  6512: at line 16
  1476: divisor is equal to zero
  6512: at line 14

See also

The PL/SQL warning PLW-06009: procedure "…" OTHERS handler does not end in RAISE or RAISE_APPLICATION_ERROR
raise_application_error is defined in dbms_standard.
The SQL Server equivalent for raise_application_error is roughly raisError.

Index