WHEN OTHERS THEN NULL

March 28, 2010

in Exceptions, PL/SQL

WHEN OTHERS THEN NULL

Look at this small piece of code which inserts a row into the EMP table.

You see an exception block in there. The questions are:

  1. What is the purpose of adding the exception handler to the code? What would happen if the exception handler were not there?
  2. What are the advantages and disadvantages of adding the WHEN OTHERS THEN NULL exception handler?
create or replace procedure pr_insert_emp is
begin
 insert into emp (
 empno
 , ename
 )
 values                 (
 10
 , 'test'
 );
exception
 when others then
 null;
end;
/

Bring on those comments.

Update (2nd April 2010): As promised, the answers to the two questions posted above.

1. What is the purpose of adding the exception handler to the code? What would happen if the exception handler were not there?

As Prakhar at comment#1 points out, WHEN OTHERS THEN NULL suppresses any error that might occur in the procedure.

If the exception handler were not there, then in case something goes wrong with the code – say, a duplicate record is inserted which violates a unique constraint – an error will be raised.

To see this in action, check out the runtime results for the procedure, with and without the exception handler.

SQL> -- With exception handler WHEN OTHERS THEN NULL, no error raised
SQL> create or replace procedure pr_insert_emp is
 2   begin
 3       insert into emp (
 4                                             empno
 5                                         , ename
 6                                       )
 7          values                 (
 8                                             10
 9                                         ,  'test'
10                                       );
11   exception
12       when others then
13           null;
14   end;
15   /

Procedure created.

SQL> exec pr_insert_emp;

PL/SQL procedure successfully completed.

SQL> -- Without exception handler WHEN OTHERS THEN NULL, PK violation  error is raised
SQL> create or replace procedure pr_insert_emp is
 2   begin
 3       insert into emp (
 4                                             empno
 5                                         , ename
 6                                       )
 7          values                 (
 8                                             10
 9                                         ,  'test'
10                                       );
11   end;
12   /

Procedure created.

SQL> exec pr_insert_emp;
BEGIN pr_insert_emp; END;

*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.PK_EMP)  violated
ORA-06512: at "SCOTT.PR_MODIFY", line 3
ORA-06512: at line 1

2. What are the advantages and disadvantages of adding the WHEN OTHERS THEN NULL exception handler?

WHEN OTHERS THEN NULL can be a very dangerous construct to put into your code. It is equivalent to ignoring the error and pretending as if nothing happened – but if an error didn’t matter enough to be reported and noticed, then there is clearly something wrong with the logic behind the code.

Oracle technologist Tom Kyte strongly opposes the use of WHEN OTHERS THEN NULL. He goes so far as call it a bug in the code. In this blog post, Tom Kyte says:

If you have code wrapped with an exception block that contains WHEN OTHERS-but does not reraise the exception-all of the code in that exception block can be safely deleted, in my opinion. You do not need it, because if the code fails to execute, you ignore the fact that it did not execute.

In the above procedure pr_insert_emp, the code would apparently be successful whether or not it inserted the record. If it does not matter either way, we need to ask: why does the procedure exist at all?

If you have written code in which WHEN OTHERS THEN NULL exists, please review it carefully. Chances are you don’t need it, and it is or could do a world of damage. Unless you have a very good reason for having it (and I’ll be most interested to know the reason, please share), remove it right away!

Note: In Oracle 11G, a new warning message (PLW-06009) has been introduced. Code containing WHEN OTHERS THEN NULL without RAISE gives a warning at compile time.

{ 6 comments… read them below or add one }

1 prakhar March 31, 2010 at 9:06 pm

1)What is the purpose of adding the exception handler to the code? What would happen if the exception handler was not there?
If error occurs while inserting ther rec(say), Your procedure will silently return to the calling block without any errors (post processing the Excep section)
If you dont specify the excep handler, then the insertion exception (say)will be propagated to the calling block, & in this block there should be any excep. handller

2)What are the advantages and disadvantages of adding the WHEN OTHERS THEN NULL exception handler?
————-

2 oratabler April 1, 2010 at 11:12 am

Hi Prakhar, Welcome here, and you’re right! The question, then, is: Is this effect of WHEN OTHERS THEN NULL a good thing or a bad thing?

3 emma June 6, 2015 at 11:32 pm

Hey there:)
What if I’m about to CREATE a backup-table for an existing one and I wanted to be able to run the script more than once.
In order to do so, I’d check if an old backup already exists and drop it if it does.
(tablename: “example”, name of the backup: “b_example”)

---begin of SQL ---
begin
execute immediate 'drop table b_example';
exception when others then null;
end;

CREATE TABLE b_example AS SELECT * FROM example;
commit;
---end of SQL ---

My question is: could the “exception when others then null;” be dangerous in any way here?
(P.S. my English isn’t perfect, please excuse me if I’ve made a mistake somewhere..)

4 oratabler June 7, 2015 at 12:33 am

Hi emma,

As a rule of thumb, only catch and escape those exceptions that are not errors in the program. In your code, the only case to handle is drop table failure because table does not exist – any other reason of code failure must be treated as an error. But by putting in WHEN OTHERS THEN NULL, you have asked your program to ignore ANY error (not limited to table does not exist), which is wrong.

Single out the ORA-00942 exception and handle just that. Here’s how:

---begin of SQL ---
declare
table_does_not_exist exception;
pragma exception_init( table_does_not_exist
, -942);
begin
begin
execute immediate 'drop table b_example';
exception
when table_does_not_exist
then null;
end;

execute immediate
'create table b_example as select * from example';
end;
---end of SQL ---

5 emma June 7, 2015 at 5:49 pm

Thanks a lot!!!

6 Damien Bastogne January 24, 2019 at 4:50 pm

On the following I just try to recuperate space from sub-partitions I’m not interested in the failure the more I can do the best it (share example)

DECLARE
CURSOR SubPartTables IS
SELECT TABLE_NAME,PARTITION_NAME, SUBPARTITION_NAME
FROM USER_TAB_SUBPARTITIONS;
aTab SubPartTables%ROWTYPE;

BEGIN
OPEN SubPartTables;

LOOP
FETCH SubPartTables INTO aTab;
EXIT WHEN SubPartTables%NOTFOUND;
BEGIN
EXECUTE IMMEDIATE ‘ALTER TABLE ‘||aTab.TABLE_NAME||’ MODIFY SUBPARTITION ‘||aTab.SUBPARTITION_NAME||’ DEALLOCATE UNUSED KEEP 1M’;
EXCEPTION WHEN OTHERS THEN NULL;
END;
END LOOP;
CLOSE SubPartTables;

Leave a Comment

Previous post:

Next post: