Search code examples
oracle-databaseplsqlprocedureout-parameters

PL SQL - Return SQLCODE as OUT parameter is accepted?


I have a procedure that returns an OUT parameter.

procedure foo (in_v IN INTEGER, out_v OUT integer)
BEGIN
...
EXCEPTION
  WHEN OTHERS THEN
    --sh*t happend
    out_v := SQLCODE;
END

That parameter will be 0 if everything goes OK, and <> 0 if something ugly happened.

Now, if sh*t happens along the way, an exception will be thrown.

Is it ok to assing the SQLCODE value to the OUT parameter ? Or is this consideres a code smell, and I will be expelled from the programming community ?

Thanks in advance.


Solution

  • If there is no additional handling of the error, I would probably advise against it. This approach just makes it necessary for each caller to examine the value of the out parameter anyway. And if the caller forgets it, a serious problem may pass unnoticed at first and create a hard to debug problem elsewhere.

    If you simply don't catch OTHERS here, you ensure that the caller has to explicitly catch it, which is a lot cleaner an easier to debug.