Sekru
Sekru

Reputation: 515

Postgres exception handling - syntax error at or near "EXCEPTION"

I'm trying to understand expections in postgresql. Maybe my problem is easy but not for me. Can someone tell me what is wrong with this query ? ERROR: syntax error at or near "EXCEPTION"

BEGIN;
select 1;
EXCEPTION 
    WHEN others THEN    
        RAISE INFO 'Caught';
END;

Upvotes: 2

Views: 2430

Answers (1)

sticky bit
sticky bit

Reputation: 37472

If you want an anonymous block you'd have to use DO. You can't just start a block in the middle of nowhere.

DO
$$
BEGIN
  SELECT 1;
EXCEPTION WHEN others THEN    
  RAISE INFO 'Caught';
END;
$$
LANGUAGE PLpgSQL;

Upvotes: 3

Related Questions