Search notes:

Oracle PL/SQL exception handling: when X or Y

create or replace package tq84_foo as

    exc_one   exception;
    exc_two   exception;
    
    pragma exception_init(exc_one, -20731);
    pragma exception_init(exc_two, -20732);

    procedure throw_up(x in number);

end tq84_foo;
/

create or replace package body tq84_foo as

    procedure throw_up(x in number) is
    begin

        if x = 1 then
           raise_application_error(-20731, 'Exception one!');
        end if;

        if x = 2 then
           raise_application_error(-20732, 'Exception two!');
        end if;

        raise_application_error(-20800, 'Other error');

    end throw_up;

end tq84_foo;
/

create or replace package tq84_bar as

    procedure proc(x in number);

end tq84_bar;
/

create or replace package body tq84_bar as

    procedure proc(x in number) is
    begin

        tq84_foo.throw_up(x);

    exception

        when tq84_foo.exc_one or tq84_foo.exc_two then

             dbms_output.put_line('tq84_bar caught ' || sqlerrm);

             if sqlcode = -20731 then
                raise;
             end if;

        when others then
        
             raise;

    end proc;

end tq84_bar;
/
Github repository Oracle-Patterns, path: /PL-SQL/exception/when_X_or_Y/install.sql
declare

  procedure p(x in number) is
  begin
    tq84_bar.proc(x);
  exception
    when tq84_foo.exc_one then
         dbms_output.put_line('run.sql caught expected exc_one');
    when others then
         dbms_output.put_line('run.sql caught other error');
  end p;


begin

  p(1);
  p(2);
  p(3);

end;
/
Github repository Oracle-Patterns, path: /PL-SQL/exception/when_X_or_Y/run.sql

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1759612689, '216.73.216.149', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/databases/Oracle/PL-SQL/exception/when-X-or-Y(122): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78