Skip to Main Content
  • Questions
  • How do I abruptly exit a function with a single command?

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Kevin.

Asked: December 21, 2004 - 1:53 pm UTC

Last updated: December 22, 2004 - 9:32 am UTC

Version: 9.2.0.5

Viewed 1000+ times

You Asked

Hi Tom,

I have been asked by one of my developer compadres how to Exit a PL/SQL function or procedure inside of a loop while the processing is happening.

In other languages there are things like "EXIT" or "END" which will effectively end a program. This is the result he seeks, and it sounds simple enough but I cannot find the answer.. and those particular commands (in PL/SQL) do things other than what I am looking for.

So, how do I abruptly exit a function with a single command?

Thanks.

and Tom said...



raise_application_error( -20001, 'Let me out of here' );


If you have "when others" exception handling

a) delete it

but in general, raise application error is going to abruptly exit back to the caller.

Rating

  (5 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Nice!

Kevin Ryan, December 21, 2004 - 2:12 pm UTC

It's a Crafty way to use existing facilities.

Seems a little bit rough around the edges though I must say.

... and if you want to exit abruptly and quietly

Gabe, December 21, 2004 - 2:15 pm UTC

"return;"



Tom Kyte
December 21, 2004 - 2:59 pm UTC

they were looking for the C equivalent of

exit(0);


-- just to terminate....

could be

Gabe, December 21, 2004 - 4:07 pm UTC

How do I abruptly exit a function with a single command?
?!?!?!?! ^^^^^^^^

Bit misleading there ... the mention of the loop did not help either!

Tom Kyte
December 21, 2004 - 7:11 pm UTC

<quote>
In other languages there are things like "EXIT" or "END" which will effectively
end a program.
</quote>

is the branch I grabbed onto and held....

No "when others" ?

Robert, December 21, 2004 - 6:34 pm UTC

Tom,

Inside "when others" we can configure our error message to send us useful debugging information such as...

1) Where in the program the error occurred (by populating a 'v_here' variable with the program location before calling SQL, etc.).
2) 'Key' values at the time of a SQL call to help in locating data on which error occurred (also by populating a 'v_what' type variable appropriatly).

We include all this info inside 'raise_application_error' including SQLERRM.

Is there a better way?

Regards,

Robert.

Tom Kyte
December 21, 2004 - 7:26 pm UTC

because 99.999999999999999999999999999999999999999999% of the time, the when others is not followed by RAISE; and the error gets silently ignored leading to heinous bugs.


The problem isn't when others so much as the developers using when others. they need to RE RAISE the error immediately -- but many times they do not, and bamm, instant bug.

I was counting on the when others to not be immediately followed by RAISE and hence this immediate exit would be an immediate "goto" instead.

Robert, December 22, 2004 - 9:32 am UTC


More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library