Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is bad in "When Others Then Null" in PL/SQL?

Tags:

plsql

I just read this question, and a solution states that:

The fact that you don't know you got the NO_DATA_FOUND exception suggests that you have made one of the biggest errors PL/SQL developers ever make:

EXCEPTION
    -- Never do this in real code!!!
   WHEN OTHERS THEN NULL;
END;

Could you explain me what is the error in this statement and what would you do to avoid doing that...

like image 705
Romain Linsolas Avatar asked Sep 17 '09 12:09

Romain Linsolas


2 Answers

The problem is, that you are catching all exceptions, and then ignoring them. You'll never know when something went wrong.

like image 84
Matthew Watson Avatar answered Nov 11 '22 07:11

Matthew Watson


There's nothing wrong with this snippet of code if you don't want the pl/sql block's exception to propagate any further for example. If you do it on purpose, it's not bad code or a mistake. That's the catch all in pl/sql. And there might be situations in code where you have nested BEGIN/EXCEPTION/END blocks and one might not want the transaction to fail just if a particular cross section of code fails. You can't state it's bad coding if you do it intentionally for whatever reason/requirement.

BEGIN

  --something important here

  --something even more important here

  BEGIN
    --something secondary goes here but not important enough to stop the process or
    --log a message about it either
    --maybe send an informative email to the support group or 
    --insert a log message when debugging the process or
    --the list could go on and on here
  EXCEPTION
    --I don't care if this block fails, absorbing all errors regardless of type
    WHEN OTHERS THEN NULL;
  END;

  -- something super important here, must happen

EXCEPTION
  WHEN NO_DATA_FOUND THEN 
    -- do something useful for this exception
  WHEN OTHERS THEN
    -- do something by default if we don't expect this error
END;
like image 41
mike-james-greco Avatar answered Nov 11 '22 07:11

mike-james-greco