Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PL/SQL exception handling: do nothing (ignore exception)

This is a question I am asked very frequently. Since I couldn't find any exact duplicate on stackoverflow, I thought I'd post it as a reference.

Question: In PL/SQL, I know how to catch exceptions and execute code when they are caught, and how to propagate them to the calling block. For example, in the following procedure, the NO_DATA_FOUND exception is handled directly, while all other exceptions are raised to the calling block:

CREATE OR REPLACE PROCEDURE MY_PROCEDURE()  IS BEGIN     do_stuff();  EXCEPTION     WHEN NO_DATA_FOUND THEN         -- Do something         handle_exception();      WHEN OTHERS THEN         -- Propagate exception         RAISE; END; 

But what command should I use to ignore one or all raised exceptions and return execution control back to the calling block?

like image 830
silentsurfer Avatar asked Dec 19 '14 13:12

silentsurfer


People also ask

How do you handle exceptions in PL SQL?

PL/SQL allows you to define your own exceptions according to the need of your program. A user-defined exception must be declared and then raised explicitly, using either a RAISE statement or the procedure DBMS_STANDARD. RAISE_APPLICATION_ERROR.

What Happens After an exception handler runs?

After an exception handler runs, the current block stops executing and the enclosing block resumes with the next statement. If there is no enclosing block, control returns to the host environment.

How would you handle exceptions and still continue to process a PL SQL procedure?

By putting a BEGIN-END block with an exception handler inside of a loop, you can continue executing the loop if some loop iterations raise exceptions. You can still handle an exception for a statement, then continue with the next statement. Place the statement in its own subblock with its own exception handlers.


1 Answers

While I agree that 99% of the time it is bad practice to silently ignore exceptions without at least logging them somewhere, there are specific situations where this is perfectly acceptable.

In these situations, NULL is your friend:

[...] EXCEPTION      WHEN OTHERS THEN         NULL; END; 

Two typical situations where ignoring exceptions might be desirable are:

1) Your code contains a statement which you know will fail occasionally and you don't want this fact to interrupt your program flow. In this case, you should enclose you statement in a nested block, as the following example shows:

CREATE OR REPLACE PROCEDURE MY_PROCEDURE()  IS     l_empoyee_name  EMPLOYEES.EMPLOYEE_NAME%TYPE; BEGIN     -- Catch potential NO_DATA_FOUND exception and continue     BEGIN          SELECT EMPLOYEE_NAME         INTO l_empoyee_name         FROM EMPLOYEES         WHERE EMPLOYEE_ID = 12345;     EXCEPTION         WHEN NO_DATA_FOUND THEN             NULL;         WHEN OTHERS THEN             RAISE;     END;      do_stuff();  EXCEPTION      WHEN OTHERS THEN         -- Propagate exception         RAISE; END; 

Note that PL/SQL generally does not allow for the On Error Resume Next type of exception handling known from Visual Basic, where all exceptions are ignored and the program continues to run as if nothing happened (see On error resume next type of error handling in PL/SQL oracle). You need to explicitly enclose potentially failing statements in a nested block.

2) Your procedure is so unimportant that ignoring all exceptions it throws will not affect your main program logic. (However, this is very rarely the case and can often result in a debugging nightmare in the long run)

BEGIN      do_stuff();  EXCEPTION      WHEN OTHERS THEN         -- Ignore all exceptions and return control to calling block         NULL; END; 
like image 131
silentsurfer Avatar answered Sep 30 '22 17:09

silentsurfer