Advanced Reporting

Error Handling in SQL

Senior Data Analyst
March 30, 2026
5 min read

PostgreSQL: Exception Blocks

DO $$

BEGIN

INSERT INTO orders (id, amount) VALUES (1, 100);

EXCEPTION WHEN unique_violation THEN

RAISE NOTICE 'Order already exists, skipping.';

END $$;

SQL Server: TRY...CATCH

BEGIN TRY

INSERT INTO orders VALUES (1, 100);

END TRY

BEGIN CATCH

PRINT ERROR_MESSAGE();

END CATCH;

*Day 90: JSON Data in SQL.*

Ready to put your knowledge into practice?

Join SQL Mastery and learn through interactive exercises.