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.
Diese Übung ist Teil des Kurses
Transactions and Error Handling in PostgreSQL
Anleitung zur Übung
- Inside of the
BEGINblock of the DO function,INSERTintopatientsthe row (a1c=7.5,glucose=null, andfasting=TRUE). - Add a
not_null_violationexception that inserts"Glucose can not be null."in the detail column of errors in case of an error.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
-- 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;