Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Similar to finally Block (JAVA) in Oracle PL/SQL Block

As in Java there is finally block which executed in all conditions.

Is there any similar function in Oracle PL/SQL which will be executed whenever procedure completes its execution even a return statement is used?

like image 880
Tajinder Avatar asked Nov 16 '25 17:11

Tajinder


2 Answers

There is no equivalent of FINALLY but you can simulate it using nested PL/SQL blocks;

DECLARE
  -- Your variables.
  return_early BOOLEAN := FALSE;
BEGIN
  -- Do something

  DECLARE
    -- Local variables in "try" block
  BEGIN 
    -- Equivalent of "try" block
    -- Do something that may raise an exception
    IF some_condition THEN
      return_early := TRUE;
      -- you could also use "GOTO end_try;" rather than surrounding the
      -- following statements in an "ELSE" statement
    ELSE
      -- Do something else that may raise an exception
    END IF;
  EXCEPTION
    WHEN your_exception THEN
      -- Equivalent of "catch" block
  END;
  <<end_try>>
  -- Handle "finally" here, after end of nested block.
  -- Note: you can only see variables declared in this outer block
  --       not variables local to the nested PL/SQL block.
  IF return_early THEN
    RETURN;
  END IF;

  -- Continue and do more stuff.
END;
/
like image 158
MT0 Avatar answered Nov 18 '25 19:11

MT0


Another thread on Stackoverflow can help out here : Exception handling in pl/sql

Where Tony says :

"You can created nested blocks:"

create or replace procedure Trial
    is 
Begin
  begin
    ---Block A--
  EXCEPTION
    when others then
      insert into error_log values('error');
  end;
  begin
    --Block B ----
  end;
end;
like image 22
khalidmehmoodawan Avatar answered Nov 18 '25 21:11

khalidmehmoodawan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!