Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 973
Pablo Oliva Avatar asked Nov 07 '17 19:11

Pablo Oliva


People also ask

How do you handle multiple exceptions with a single except clause?

By handling multiple exceptions, a program can respond to different exceptions without terminating it. In Python, try-except blocks can be used to catch and respond to one or multiple exceptions. In cases where a process raises more than one possible exception, they can all be handled using a single except clause.

Can you throw multiple exceptions in one throw statement?

You can't throw two exceptions. I.e. you can't do something like: try { throw new IllegalArgumentException(), new NullPointerException(); } catch (IllegalArgumentException iae) { // ... } catch (NullPointerException npe) { // ... }

Can we have more than one exception in Oracle?

Yes , its possible to mention multiple exception handlers in one block.

How do you add multiple exceptions in single catch block?

Java allows you to catch multiple type exceptions in a single catch block. It was introduced in Java 7 and helps to optimize code. You can use vertical bar (|) to separate multiple exceptions in catch block.


1 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.

like image 191
Sentinel Avatar answered Sep 23 '22 13:09

Sentinel