특정 예외 캡처하기
DO 함수에서 glucose가 null로 설정되는 상황을 포착하고, Glucose는 null이 될 수 없다고 명확히 기록하는 로그 메시지를 남기도록 만들어 보세요.
이 연습은 강의의 일부입니다
PostgreSQL에서의 트랜잭션과 오류 처리
연습 안내
- DO 함수의
BEGIN블록 안에서patients테이블에 (a1c=7.5,glucose=null,fasting=TRUE) 행을INSERT하세요. - 오류가 발생할 경우
errors의 detail 열에"Glucose can not be null."를 삽입하도록not_null_violation예외 처리를 추가하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
-- 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;