3

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.

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

You probably also want SAVEPOINTs to rollback the step that generated the error, instead of the whole transaction

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.