Can a single 'when' clause handle multiple exception types in oracle?

Viewed 3254

Suppose I have a procedure as follows:

PROCEDURE proc_name (args)
IS

  --  declarations
    ...
BEGIN

    -- code
    ...
EXCEPTION

    WHEN an_error THEN

        --error handling code
         ....
        WHEN another_error THEN

        -- error handling code, identical to the one for an_error
         ...
     WHEN others THEN
       ---generic error handling code`
       ....
END;

Ideally, I would like to be able to catch both an_error and another_error in the same WHEN clause, since their handling is identical.

Is this possible in Oracle? If not, what are other possibilities to avoid code duplication?

2 Answers

Yes you can.

You can use OR conditions between the exceptions so

EXCEPTION
  WHEN an_exception 
    OR another_exception
  THEN
    handle it here;
END;

See The Docs for more details on exception handling.

Yes.

But not for OTHERS exception. It can not be merged with another exception.

Related