Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Try Catch Like Exception Handling In Pl/Sql

Tags:

In a procedure, I want to do logic unit 1, doesn't matter if it fails, execute logic unit 2

this seems like a typical try-catch scenario. but how can I do it in pl/sql?

create or replace
PACKAGE BUILD_PKG
AS
PROCEDURE reset_seq_and_truncate_tbl(
    p_seq_name   IN VARCHAR2,
    p_table_name IN VARCHAR2 );
END BUILD_PKG;
/
create or replace
PACKAGE BODY  BUILD_PKG
AS
BEGIN
PROCEDURE reset_seq_and_truncate_tbl(
    p_seq_name   IN VARCHAR2,
    p_table_name IN VARCHAR2 )
 IS
   l_val NUMBER;
 BEGIN
   BEGIN
     EXECUTE immediate 'truncate table ' || p_table_name;
   EXCEPTION 
        WHEN OTHERS 
        THEN dbms_output.put_line(SQLCODE);
   END;

   BEGIN
     EXECUTE immediate 'alter sequence ' || p_seq_name || ' increment by 1 minvalue 0';
   EXCEPTION 
        WHEN OTHERS 
        THEN dbms_output.put_line(SQLCODE);
   END; 
 END reset_seq_and_truncate_tbl;

END BUILD_PKG;

as you can see the second Exception block cause the problem.

like image 865
Junchen Liu Avatar asked Sep 21 '12 09:09

Junchen Liu


People also ask

What are the 3 types of exceptions in Plsql?

There are three types of exceptions: Predefined exceptions are error conditions that are defined by PL/SQL. Non-predefined exceptions include any standard TimesTen errors. User-defined exceptions are exceptions specific to your application.

What are PL SQL cursor exceptions?

An exception is an error which disrupts the normal flow of program instructions. PL/SQL provides us the exception block which raises the exception thus helping the programmer to find out the fault and resolve it. There are two types of exceptions defined in PL/SQL. User defined exception. System defined exceptions.


1 Answers

Enclose the second EXCEPTION in BEGIN..END block. Try this way

PROCEDURE reset_seq_and_truncate_tbl(
    p_seq_name   IN VARCHAR2,
    p_table_name IN VARCHAR2 )
 IS
   l_val NUMBER;
 BEGIN
   EXECUTE immediate 'truncate table ' || p_table_name;
 EXCEPTION 
      WHEN OTHERS 
      THEN
      BEGIN
        dbms_output.put_line(SQLCODE);
        EXECUTE immediate 'alter sequence ' || p_seq_name || ' increment by 1 minvalue 0';
       --this would cause compilation error
       EXCEPTION 
            WHEN OTHERS 
            THEN dbms_output.put_line(SQLCODE);
     END; 
 END reset_seq_and_truncate_tbl;

UPDATE in response to the comment

PROCEDURE reset_seq_and_truncate_tbl(
    p_seq_name   IN VARCHAR2,
    p_table_name IN VARCHAR2 )
 IS
   l_val NUMBER;
 BEGIN
   BEGIN
     EXECUTE immediate 'truncate table ' || p_table_name;
   EXCEPTION 
        WHEN OTHERS 
        THEN dbms_output.put_line(SQLCODE);
   END;

   BEGIN
     EXECUTE immediate 'alter sequence ' || p_seq_name || ' increment by 1 minvalue 0';
   EXCEPTION 
        WHEN OTHERS 
        THEN dbms_output.put_line(SQLCODE);
   END; 
 END reset_seq_and_truncate_tbl;
like image 158
Nick Krasnov Avatar answered Oct 06 '22 20:10

Nick Krasnov