TRY CATCH AND ROLLBACK in Oracle Syntax

I have a sql server stored procedure that uses a try catch block and then does a rollback if an error occurs. Can someone show me how to do the same thing in Oracle Syntax?

CREATE OR REPLACE PROCEDURE MYPROC

AS

BEGIN TRY -- Close to top of my stored procedure



END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION


END CATCH
LVL 2
brgdotnetcontractorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sdstuberCommented:
try/catch corresponds to BEGIN/EXCEPTION/END.

CREATE OR REPLACE PROCEDURE myproc
AS
BEGIN
    NULL;
--- do something more interesting than NULL here

EXCEPTION
    WHEN OTHERS  -- OTHERS is the catchall for exceptions, you can list individual exceptions by name each with their own WHEN...THEN clause
    THEN
        NULL;
-- handle the exception with something other than NULL here

END;

Open in new window


unfortunately there is no direct correlation to try/catch/finally
you can mimic it by nesting begin/exception/end blocks inside other blocks but it's not as clean of an api

You can find a list of predefined named-exceptions here

https://docs.oracle.com/database/121/LNPLS/errors.htm#GUID-8C327B4A-71FA-4CFB-8BC9-4550A23734D6

you can define your own named exceptions with the EXCEPTION keyword, as well as give names to specific error codes with PRAGMA

https://docs.oracle.com/database/121/LNPLS/errors.htm#LNPLS99871
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brgdotnetcontractorAuthor Commented:
Thank you. Apologies for the delay.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.