Nawaz Sharif
Nawaz Sharif

Reputation: 59

postgres exception handling

I have a postgresql function with multiple queries to validate table data.But if error occur in one of queries than whole process of function get stop. But i want that, it should print the error by raise warning the error than escape the error and continue execution of other queries.

--Begin function
   BEGIN
    ----- query 1--------
   EXCEPTION 
     WHEN others THEN    
       ------------------'Exception in query 1';------------
   END;

   BEGIN
    ------------------------ Query 2---------------------
   EXCEPTION 
     WHEN others THEN    
      -------------------'Exception in query 2';-------------------
   END;
--End function

this is the code in which i am using exception but i have more than 100 queries in the function so i have have to put each queries in the exception block.And i am asking for some other easy way to solve the problem.

Upvotes: 3

Views: 1843

Answers (1)

Ubercool
Ubercool

Reputation: 1021

You can use nested block to handle exception thrown by certain pieces of your code like below:

--Begin function
   BEGIN
    --Validation query 1
   EXCEPTION 
     WHEN others THEN    
       RAISE INFO 'Exception in query 1';
   END;

   BEGIN
    -- Query 2
   EXCEPTION 
     WHEN others THEN    
       RAISE INFO 'Exception in query 2';
   END;
--End function

Upvotes: 4

Related Questions