ComenzarEmpieza gratis

Capturing specific exceptions

Let's build a DO function that captures when glucose is set to null, and logs a message stating explicitly that Glucose can not be null.

Este ejercicio forma parte del curso

Transactions and Error Handling in PostgreSQL

Ver curso

Instrucciones del ejercicio

  • Inside of the BEGIN block of the DO function, INSERT into patients the row (a1c=7.5, glucose=null, and fasting= TRUE).
  • Add a not_null_violation exception that inserts "Glucose can not be null." in the detail column of errors in case of an error.

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

-- Make a DO function
DO $$
-- Open a transaction block
BEGIN
    INSERT INTO patients (a1c, glucose, fasting) 
    VALUES (7.5, ___, TRUE);
-- Catch an Exception                                                               
EXCEPTION
	-- Make it catch not_null_constraint exception types
    WHEN ___ ___
    	-- Insert the proper msg and detail
       INSERT INTO errors (___, detail) 
       VALUES ('failed to insert', '___');
END$$;
                                                                     
-- Select all the errors recorded
SELECT * FROM errors;
Editar y ejecutar código